Skip to content

Commit a93e9cf

Browse files
authored
Improved fetch docs in globals.md
1 parent 47a59bd commit a93e9cf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

doc/api/globals.md

+33
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,39 @@ changes:
531531
532532
A browser-compatible implementation of the [`fetch()`][] function.
533533

534+
```mjs
535+
const res = await fetch('https://nodejs.org/api/documentation.json');
536+
if (res.ok) {
537+
const data = await res.json();
538+
console.log(data);
539+
}
540+
```
541+
542+
The implementation is based upon [undici](https://undici.nodejs.org), an HTTP/1.1 client written from scratch for Node.js. You can figure out which version of `undici` is bundled in your Node.js process reading the `process.versions.undici` property.
543+
544+
## Custom dispatcher
545+
546+
You can use a custom dispatcher to dispatch requests passing it in fetch's options object.
547+
The dispatcher must be compatible with `undici`'s [`Dispatcher` class](https://undici.nodejs.org/#/docs/api/Dispatcher.md).
548+
549+
```js
550+
fetch(url, { dispatcher: new MyAgent() });
551+
```
552+
553+
It is possible to change the global dispatcher in Node.js using:
554+
555+
```js
556+
globalThis[Symbol.for('undici.globalDispatcher.1')] = yourDispatcher;
557+
```
558+
559+
## Related classes
560+
561+
The following globals are available to use with `fetch`:
562+
- [`FormData`](https://nodejs.org/api/globals.html#class-formdata)
563+
- [`Headers`](https://nodejs.org/api/globals.html#class-headers)
564+
- [`Request`](https://nodejs.org/api/globals.html#request)
565+
- [`Response`](https://nodejs.org/api/globals.html#response).
566+
534567
## Class: `File`
535568

536569
<!-- YAML

0 commit comments

Comments
 (0)