fix: cancel request example import (#1513)

* Fix example

* Fix formatting

Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>

Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>
This commit is contained in:
Software and Outsourcing 2022-03-01 10:54:27 +01:00 committed by GitHub
parent 5e78af3ba7
commit 61b3b5a063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 3 deletions

View File

@ -470,7 +470,7 @@ You may cancel requests with `AbortController`. A suggested implementation is [`
An example of timing out a request after 150ms could be achieved as the following:
```js
import fetch from 'node-fetch';
import fetch, { AbortError } from 'node-fetch';
// AbortController was added in node v14.17.0 globally
const AbortController = globalThis.AbortController || await import('abort-controller')
@ -484,7 +484,7 @@ try {
const response = await fetch('https://example.com', {signal: controller.signal});
const data = await response.json();
} catch (error) {
if (error instanceof fetch.AbortError) {
if (error instanceof AbortError) {
console.log('request was aborted');
}
} finally {
@ -868,4 +868,4 @@ Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid
[error-handling.md]: https://github.com/node-fetch/node-fetch/blob/master/docs/ERROR-HANDLING.md
[FormData]: https://developer.mozilla.org/en-US/docs/Web/API/FormData
[Blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob
[File]: https://developer.mozilla.org/en-US/docs/Web/API/File
[File]: https://developer.mozilla.org/en-US/docs/Web/API/File