source: node_modules/node-abort-controller/README.md@ 65b6638

main
Last change on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.1 KB
Line 
1# node-abort-controller
2
3AbortController Polyfill for Node.JS based on EventEmitter for Node v14.6.x and below.
4
5Are you using Node 14.7.0 or above? You don't need this! [Node has `AbortController` and `AbortSignal` as builtin globals](https://nodejs.org/dist/latest/docs/api/globals.html#globals_class_abortcontroller). In Node versions >=14.7.0 and <15.4.0 you can access the experimental implementation using `--experimental-abortcontroller`.
6
7## Example Usage
8
9### Timing out `fetch`
10
11```javascript
12import fetch from "node-fetch";
13import { AbortController } from "node-abort-controller";
14
15const controller = new AbortController();
16const signal = controller.signal;
17
18await fetch("https:/www.google.com", { signal });
19
20// Abort fetch after 500ms. Effectively a timeout
21setTimeout(() => controller.abort(), 500);
22```
23
24### Re-usable `fetch` function with a built in timeout
25
26```javascript
27import { AbortController } from "node-abort-controller";
28import fetch from "node-fetch";
29
30const fetchWithTimeout = async (url = "") => {
31 const controller = new AbortController();
32 const { signal } = controller;
33
34 const timeout = setTimeout(() => {
35 controller.abort();
36 }, 5000);
37
38 const request = await fetch(url, { signal });
39
40 clearTimeout(timeout);
41
42 const result = await req.json();
43
44 return result;
45};
46```
47
48## Why would I need this?
49
50You might not need to! Generally speaking, there are three environments your JavaScript code can run in:
51
52- Node
53- Modern Browsers (Not Internet Explorer)
54- Legacy Browsers (Mostly Internet Explorer)
55
56For modern JS APIs, each environment would ideally get a polyfill:
57
58- only if it needs one
59- specific to the platform.
60
61In practice, this is hard. Tooling such as webpack and browserify are great at making sure stuff works out of the box in all environments. But it is quite easy to fail on both points above. In all likelyhood, you end up shipping less than ideal polyfills on platforms that don't even need them. So what is a developer to do? In the case of `fetch` and `AbortController` I've done the work for you. This is a guide to that work.
62
63If you are building a ...
64
65#### NodeJS library only supports Node 16 or above
66
67You don't need this library! [`AbortController` is now built into nodeJS ](https://nodejs.org/api/globals.html#globals_class_abortcontroller). Use that instead.
68
69#### Web Application running only in modern browsers
70
71You don't need a library! Close this tab. Uninstall this package.
72
73#### Web Application running in modern browsers AND NodeJS (such as a server side rendered JS app)
74
75Use _this package_ and [node-fetch](https://www.npmjs.com/package/node-fetch). It is minimally what you need.
76
77#### Web Application supporting legacy browsers AND NOT NodeJS
78
79Use [abort-controller](https://www.npmjs.com/package/abort-controller) and [whatwg-fetch](https://www.npmjs.com/package/whatwg-fetch). These are more complete polyfills that will work in all browser environments.
80
81#### Web Application supporting legacy browsers AND NodeJS
82
83Use [abort-controller](https://www.npmjs.com/package/abort-controller) and [cross-fetch](https://www.npmjs.com/package/cross-fetch). Same as above, except cross-fetch will polyfill correctly in both the browser and node.js
84
85#### NodeJS Library being consumed by other applications and using `fetch` internally
86
87Use _this package_ and [node-fetch](https://www.npmjs.com/package/node-fetch). It is the smallest and least opinionated combination for your end users. Application developers targeting Internet Exploer will need to polyfill `AbortController` and `fetch` on their own. But your library won't be forcing unecessary polyfills on developers who only target modern browsers.
88
89## Goals
90
91With the above guide in mind, this library has a very specific set of goals:
92
931. Provide a minimal polyfill in node.js
942. Do not provide a polyfill in any browser environment
95
96This is the ideal for _library authors_ who use `fetch` and `AbortController` internally and target _both_ browser and node developers.
97
98## Prior Art
99
100Thank you @mysticatea for https://github.com/mysticatea/abort-controller. It is a fantastic `AbortController` polyfill and ideal for many use cases.
Note: See TracBrowser for help on using the repository browser.