diff --git a/README.md b/README.md index 32cc3c20..3024c5a1 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ expected to uphold this code. * [Sending cookies](#sending-cookies) * [Receiving cookies](#receiving-cookies) * [Obtaining the Response URL](#obtaining-the-response-url) + * [Aborting fetches](#aborting-fetches) * [Browser Support](#browser-support) ## Read this first @@ -260,6 +261,47 @@ response.headers['X-Request-URL'] = request.url This server workaround is necessary if you need reliable `response.url` in Firefox < 32, Chrome < 37, Safari, or IE. +#### Aborting requests + +This polyfill supports +[the abortable fetch API](https://developers.google.com/web/updates/2017/09/abortable-fetch). +However, aborting a fetch requires use of two additional DOM APIs: +[AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) +and +[AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal). +Typically, browsers that do not support fetch will also not support +AbortController or AbortSignal. + +Consequently, you will need to find a polyfill for these APIs to abort fetches. + +Once you have an AbortController and AbortSignal polyfill in place, you can +abort a fetch like so: + +```js +const controller = new AbortController() + +fetch('/avatars', { + method: 'POST', + body: data, + signal: controller.signal +}) + .then( + function(response) { + console.log('request succeeded', response) + }, + function(ex) { + if (ex.name === 'AbortError') { + console.log('request aborted') + } else { + console.log('request failed, but it was not aborted') + } + } + ) + +// some time later... +controller.abort(); +``` + ## Browser Support - Chrome