Skip to content

Commit a13ec4b

Browse files
committed
doc: add differences from standard fetch in fetch() API
1 parent 2ea31e5 commit a13ec4b

File tree

1 file changed

+82
-56
lines changed

1 file changed

+82
-56
lines changed

doc/api/globals.md

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ These objects are available in all modules.
1111
The following variables may appear to be global but are not. They exist only in
1212
the scope of [CommonJS modules][]:
1313

14-
* [`__dirname`][]
15-
* [`__filename`][]
16-
* [`exports`][]
17-
* [`module`][]
18-
* [`require()`][]
14+
- [`__dirname`][]
15+
- [`__filename`][]
16+
- [`exports`][]
17+
- [`module`][]
18+
- [`require()`][]
1919

2020
The objects listed here are specific to Node.js. There are [built-in objects][]
2121
that are part of the JavaScript language itself, which are also globally
@@ -39,12 +39,13 @@ The API is based on the Web API {AbortController}.
3939
```js
4040
const ac = new AbortController();
4141

42-
ac.signal.addEventListener('abort', () => console.log('Aborted!'),
43-
{ once: true });
42+
ac.signal.addEventListener('abort', () => console.log('Aborted!'), {
43+
once: true,
44+
});
4445

4546
ac.abort();
4647

47-
console.log(ac.signal.aborted); // Prints true
48+
console.log(ac.signal.aborted); // Prints true
4849
```
4950

5051
### `abortController.abort([reason])`
@@ -61,7 +62,7 @@ changes:
6162
description: Added the new optional reason argument.
6263
-->
6364

64-
* `reason` {any} An optional reason, retrievable on the `AbortSignal`'s
65+
- `reason` {any} An optional reason, retrievable on the `AbortSignal`'s
6566
`reason` property.
6667

6768
Triggers the abort signal, causing the `abortController.signal` to emit
@@ -75,7 +76,7 @@ added:
7576
- v14.17.0
7677
-->
7778

78-
* Type: {AbortSignal}
79+
- Type: {AbortSignal}
7980

8081
### Class: `AbortSignal`
8182

@@ -85,7 +86,7 @@ added:
8586
- v14.17.0
8687
-->
8788

88-
* Extends: {EventTarget}
89+
- Extends: {EventTarget}
8990

9091
The `AbortSignal` is used to notify observers when the
9192
`abortController.abort()` method is called.
@@ -104,8 +105,8 @@ changes:
104105
description: Added the new optional reason argument.
105106
-->
106107

107-
* `reason` {any}
108-
* Returns: {AbortSignal}
108+
- `reason` {any}
109+
- Returns: {AbortSignal}
109110

110111
Returns a new already aborted `AbortSignal`.
111112

@@ -117,7 +118,7 @@ added:
117118
- v16.14.0
118119
-->
119120

120-
* `delay` {number} The number of milliseconds to wait before triggering
121+
- `delay` {number} The number of milliseconds to wait before triggering
121122
the AbortSignal.
122123

123124
Returns a new `AbortSignal` which will be aborted in `delay` milliseconds.
@@ -130,7 +131,7 @@ added:
130131
- v18.17.0
131132
-->
132133

133-
* `signals` {AbortSignal\[]} The `AbortSignal`s of which to compose a new `AbortSignal`.
134+
- `signals` {AbortSignal\[]} The `AbortSignal`s of which to compose a new `AbortSignal`.
134135

135136
Returns a new `AbortSignal` which will be aborted if any of the provided
136137
signals are aborted. Its [`abortSignal.reason`][] will be set to whichever
@@ -155,9 +156,13 @@ const ac = new AbortController();
155156
ac.signal.onabort = () => console.log('aborted!');
156157

157158
// Or the EventTarget API...
158-
ac.signal.addEventListener('abort', (event) => {
159-
console.log(event.type); // Prints 'abort'
160-
}, { once: true });
159+
ac.signal.addEventListener(
160+
'abort',
161+
(event) => {
162+
console.log(event.type); // Prints 'abort'
163+
},
164+
{ once: true }
165+
);
161166

162167
ac.abort();
163168
```
@@ -181,7 +186,7 @@ added:
181186
- v14.17.0
182187
-->
183188

184-
* Type: {boolean} True after the `AbortController` has been aborted.
189+
- Type: {boolean} True after the `AbortController` has been aborted.
185190

186191
#### `abortSignal.onabort`
187192

@@ -191,7 +196,7 @@ added:
191196
- v14.17.0
192197
-->
193198

194-
* Type: {Function}
199+
- Type: {Function}
195200

196201
An optional callback function that may be set by user code to be notified
197202
when the `abortController.abort()` function has been called.
@@ -204,14 +209,14 @@ added:
204209
- v16.14.0
205210
-->
206211

207-
* Type: {any}
212+
- Type: {any}
208213

209214
An optional reason specified when the `AbortSignal` was triggered.
210215

211216
```js
212217
const ac = new AbortController();
213218
ac.abort(new Error('boom!'));
214-
console.log(ac.signal.reason); // Error: boom!
219+
console.log(ac.signal.reason); // Error: boom!
215220
```
216221

217222
#### `abortSignal.throwIfAborted()`
@@ -238,7 +243,7 @@ See {Blob}.
238243
added: v0.1.103
239244
-->
240245

241-
* Type: {Function}
246+
- Type: {Function}
242247

243248
Used to handle binary data. See the [buffer section][].
244249

@@ -348,7 +353,7 @@ A browser-compatible implementation of [`CompressionStream`][].
348353
added: v0.1.100
349354
-->
350355

351-
* Type: {Object}
356+
- Type: {Object}
352357

353358
Used to print to stdout and stderr. See the [`console`][] section.
354359

@@ -541,6 +546,15 @@ The implementation is based upon [undici](https://undici.nodejs.org), an HTTP/1.
541546
written from scratch for Node.js. You can figure out which version of `undici` is bundled
542547
in your Node.js process reading the `process.versions.undici` property.
543548

549+
### Differences from standard fetch
550+
551+
Node.js provides a `fetch()` implementation that is similar to the web standard but with a few notable differences:
552+
553+
- `new Response(asyncIterable)` is supported, which is not in the standard.
554+
- Cookies are not automatically handled.
555+
- The forbidden headers list is not enforced.
556+
- Other subtle differences may exist; consult the implementation details.
557+
544558
### Custom dispatcher
545559

546560
You can use a custom dispatcher to dispatch requests passing it in fetch's options object.
@@ -564,10 +578,10 @@ setGlobalDispatcher(new MyAgent());
564578

565579
The following globals are available to use with `fetch`:
566580

567-
* [`FormData`](https://nodejs.org/api/globals.html#class-formdata)
568-
* [`Headers`](https://nodejs.org/api/globals.html#class-headers)
569-
* [`Request`](https://nodejs.org/api/globals.html#request)
570-
* [`Response`](https://nodejs.org/api/globals.html#response).
581+
- [`FormData`](https://nodejs.org/api/globals.html#class-formdata)
582+
- [`Headers`](https://nodejs.org/api/globals.html#class-headers)
583+
- [`Request`](https://nodejs.org/api/globals.html#request)
584+
- [`Response`](https://nodejs.org/api/globals.html#response).
571585

572586
## Class: `File`
573587

@@ -603,7 +617,7 @@ added: v0.1.27
603617

604618
> Stability: 3 - Legacy. Use [`globalThis`][] instead.
605619
606-
* Type: {Object} The global namespace object.
620+
- Type: {Object} The global namespace object.
607621

608622
In browsers, the top-level scope has traditionally been the global scope. This
609623
means that `var something` will define a new global variable, except within
@@ -702,13 +716,15 @@ A partial implementation of [`window.navigator`][].
702716
added: v21.0.0
703717
-->
704718

705-
* Type: {number}
719+
- Type: {number}
706720

707721
The `navigator.hardwareConcurrency` read-only property returns the number of
708722
logical processors available to the current Node.js instance.
709723

710724
```js
711-
console.log(`This process is running on ${navigator.hardwareConcurrency} logical processors`);
725+
console.log(
726+
`This process is running on ${navigator.hardwareConcurrency} logical processors`
727+
);
712728
```
713729

714730
### `navigator.language`
@@ -717,7 +733,7 @@ console.log(`This process is running on ${navigator.hardwareConcurrency} logical
717733
added: v21.2.0
718734
-->
719735

720-
* Type: {string}
736+
- Type: {string}
721737

722738
The `navigator.language` read-only property returns a string representing the
723739
preferred language of the Node.js instance. The language will be determined by
@@ -729,7 +745,9 @@ The value is representing the language version as defined in [RFC 5646][].
729745
The fallback value on builds without ICU is `'en-US'`.
730746

731747
```js
732-
console.log(`The preferred language of the Node.js instance has the tag '${navigator.language}'`);
748+
console.log(
749+
`The preferred language of the Node.js instance has the tag '${navigator.language}'`
750+
);
733751
```
734752

735753
### `navigator.languages`
@@ -738,7 +756,7 @@ console.log(`The preferred language of the Node.js instance has the tag '${navig
738756
added: v21.2.0
739757
-->
740758

741-
* Type: {Array<string>}
759+
- Type: {Array<string>}
742760

743761
The `navigator.languages` read-only property returns an array of strings
744762
representing the preferred languages of the Node.js instance.
@@ -758,7 +776,7 @@ console.log(`The preferred languages are '${navigator.languages}'`);
758776
added: v21.2.0
759777
-->
760778

761-
* Type: {string}
779+
- Type: {string}
762780

763781
The `navigator.platform` read-only property returns a string identifying the
764782
platform on which the Node.js instance is running.
@@ -773,7 +791,7 @@ console.log(`This process is running on ${navigator.platform}`);
773791
added: v21.1.0
774792
-->
775793

776-
* Type: {string}
794+
- Type: {string}
777795

778796
The `navigator.userAgent` read-only property returns user agent
779797
consisting of the runtime name and major version number.
@@ -804,29 +822,37 @@ await navigator.locks.request('my_resource', async (lock) => {
804822
});
805823

806824
// Request a shared lock
807-
await navigator.locks.request('shared_resource', { mode: 'shared' }, async (lock) => {
808-
// Multiple shared locks can be held simultaneously
809-
console.log(`Shared lock acquired: ${lock.name}`);
810-
});
825+
await navigator.locks.request(
826+
'shared_resource',
827+
{ mode: 'shared' },
828+
async (lock) => {
829+
// Multiple shared locks can be held simultaneously
830+
console.log(`Shared lock acquired: ${lock.name}`);
831+
}
832+
);
811833
```
812834

813835
```cjs
814836
// Request an exclusive lock
815-
navigator.locks.request('my_resource', async (lock) => {
816-
// The lock has been acquired.
817-
console.log(`Lock acquired: ${lock.name}`);
818-
// Lock is automatically released when the function returns
819-
}).then(() => {
820-
console.log('Lock released');
821-
});
837+
navigator.locks
838+
.request('my_resource', async (lock) => {
839+
// The lock has been acquired.
840+
console.log(`Lock acquired: ${lock.name}`);
841+
// Lock is automatically released when the function returns
842+
})
843+
.then(() => {
844+
console.log('Lock released');
845+
});
822846

823847
// Request a shared lock
824-
navigator.locks.request('shared_resource', { mode: 'shared' }, async (lock) => {
825-
// Multiple shared locks can be held simultaneously
826-
console.log(`Shared lock acquired: ${lock.name}`);
827-
}).then(() => {
828-
console.log('Shared lock released');
829-
});
848+
navigator.locks
849+
.request('shared_resource', { mode: 'shared' }, async (lock) => {
850+
// Multiple shared locks can be held simultaneously
851+
console.log(`Shared lock acquired: ${lock.name}`);
852+
})
853+
.then(() => {
854+
console.log('Shared lock released');
855+
});
830856
```
831857

832858
See [`worker.locks`][] for detailed API documentation.
@@ -895,7 +921,7 @@ The [`perf_hooks.performance`][] object.
895921
added: v0.1.7
896922
-->
897923

898-
* Type: {Object}
924+
- Type: {Object}
899925

900926
The process object. See the [`process` object][] section.
901927

@@ -905,7 +931,7 @@ The process object. See the [`process` object][] section.
905931
added: v11.0.0
906932
-->
907933

908-
* `callback` {Function} Function to be queued.
934+
- `callback` {Function} Function to be queued.
909935

910936
The `queueMicrotask()` method queues a microtask to invoke `callback`. If
911937
`callback` throws an exception, the [`process` object][] `'uncaughtException'`
@@ -1244,7 +1270,7 @@ The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section.
12441270
added: v8.0.0
12451271
-->
12461272

1247-
* Type: {Object}
1273+
- Type: {Object}
12481274

12491275
The object that acts as the namespace for all W3C
12501276
[WebAssembly][webassembly-org] related functionality. See the

0 commit comments

Comments
 (0)