File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -498,6 +498,46 @@ changes:
498
498
499
499
A browser-compatible implementation of the [ ` fetch() ` ] [ ] function.
500
500
501
+ ``` mjs
502
+ const res = await fetch (' https://nodejs.org/api/documentation.json' );
503
+ if (res .ok ) {
504
+ const data = await res .json ();
505
+ console .log (data);
506
+ }
507
+ ```
508
+
509
+ The implementation is based upon [ undici] ( https://undici.nodejs.org ) , an HTTP/1.1 client
510
+ written from scratch for Node.js. You can figure out which version of ` undici ` is bundled
511
+ in your Node.js process reading the ` process.versions.undici ` property.
512
+
513
+ ## Custom dispatcher
514
+
515
+ You can use a custom dispatcher to dispatch requests passing it in fetch's options object.
516
+ The dispatcher must be compatible with ` undici ` 's
517
+ [ ` Dispatcher ` class] ( https://undici.nodejs.org/#/docs/api/Dispatcher.md ) .
518
+
519
+ ``` js
520
+ fetch (url, { dispatcher: new MyAgent () });
521
+ ```
522
+
523
+ It is possible to change the global dispatcher in Node.js installing ` undici ` and using
524
+ the ` setGlobalDispatcher() ` method. Calling this method will affect both ` undici ` and
525
+ Node.js.
526
+
527
+ ``` mjs
528
+ import { setGlobalDispatcher } from ' undici' ;
529
+ setGlobalDispatcher (new MyAgent ());
530
+ ```
531
+
532
+ ## Related classes
533
+
534
+ The following globals are available to use with ` fetch ` :
535
+
536
+ * [ ` FormData ` ] ( https://nodejs.org/api/globals.html#class-formdata )
537
+ * [ ` Headers ` ] ( https://nodejs.org/api/globals.html#class-headers )
538
+ * [ ` Request ` ] ( https://nodejs.org/api/globals.html#request )
539
+ * [ ` Response ` ] ( https://nodejs.org/api/globals.html#response ) .
540
+
501
541
## Class: ` File `
502
542
503
543
<!-- YAML
You can’t perform that action at this time.
0 commit comments