abortcontroller-polyfill
Advanced tools
Comparing version 1.0.1 to 1.0.2
(function(self) { | ||
'use strict'; | ||
if (self.AbortController) { | ||
return; | ||
} | ||
class AbortController { | ||
@@ -5,0 +9,0 @@ constructor() { |
{ | ||
"name": "abortcontroller-polyfill", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Browser polyfill for the AbortController DOM API (stub that calls catch, doesn't actually abort request).", | ||
@@ -5,0 +5,0 @@ "main": "abortcontroller.js", |
# AbortController "polyfill" | ||
Minimal stubs so that the AbortController API for terminating ```fetch()``` requests can be used in | ||
browsers that doesn't yet implement it. This "polyfill" doesn't actually close the connection when | ||
the request is aborted, but it calls ```.catch()``` with ```err.name == 'AbortError'``` instead of | ||
```.then()```. | ||
Minimal stubs so that the AbortController DOM API for terminating ```fetch()``` requests can be used | ||
in browsers that doesn't yet implement it. This "polyfill" doesn't actually close the connection | ||
when the request is aborted, but it calls ```.catch()``` with ```err.name == 'AbortError'``` instead | ||
of ```.then()```. | ||
```js | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
fetch('/some/url', {signal}).then(res => res.json()).then(data => { | ||
// do something with "data" | ||
}).catch(err => { | ||
if (err.name == 'AbortError') { | ||
return; | ||
} | ||
}); | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
fetch('/some/url', {signal}).then(res => res.json()).then(data => { | ||
// do something with "data" | ||
}).catch(err => { | ||
if (err.name == 'AbortError') { | ||
return; | ||
} | ||
}); | ||
``` | ||
@@ -29,3 +29,3 @@ | ||
If you're using webpack or similar, import it early in your client entrypoint .js file using | ||
```require('abortcontroller-polyfill')``` or ```import 'abortcontroller-polyfill'```. | ||
```import 'abortcontroller-polyfill'``` or ```require('abortcontroller-polyfill')```. | ||
@@ -32,0 +32,0 @@ |
3396
34