You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the minimum bundle size I prefer the first one and because we based on the standard AbortController API we do not need to polyfill fetch additionally:
npm i abortcontroller-polyfill
yarn add abortcontroller-polyfill
pnpm add abortcontroller-polyfill
Do not forget to exclude transpiled polyfill code from the Babel transpile: exclude: [ "node_modules/**" ], or exclude: [ "node_modules/abortcontroller-polyfill/**" ],
Usage example
letabortController// abort can be called as many times as you want (it run only ones)constabort=()=>abortController&&abortController.abort()constdoFetch=()=>{abort()// abort the previous / ongoing callabortController=newAbortController()fetch('http://api.plos.org/search?q=title:DNA',{credentials: 'same-origin',signal: abortController.signal}).then(response=>{if(!response.ok){thrownewError(`${response.status}${response.statusText}`)}returnresponse.json()}).then(data=>{console.log('REQUEST FINISHED')console.dir(data)}).catch(error=>{if(error.name==='AbortError'){console.log('REQUEST ABORTED')}else{console.error(`REQUEST FAILED: ${error ? error.message : ''}`)}/* The alternative way to distinct the AbortError */if(abortController.signal.aborted){console.log('REQUEST ABORTED')}else{console.error(`REQUEST FAILED: ${error ? error.message : ''}`)}})}doFetch()setTimeout(abort,5000)
Resolves #54
Alternative to #68, and #137
This solution
abort()for the several parallel fetch requestsPromise.racesurrogates)AbortControllerimplementation (can run with polyfills on IE, see below)readyState === DONE_STATE)includebut forsame-originalso (usefull for some CORS use cases)unfetchenchancement and this implementation alternativeNot sure about 500 bytes of the bundle size, but should be very close to it 😉
The code (
abortable-unfetch.mjs)The polyfill (
abortable-unfetch-polyfill.mjs)AbortControllerpolyfillsThere are two alternatives to polyfill the standard
AbortControllerbehaviour on the old browsers (like IE11):abortcontroller-polyfillabort-controllerFor the minimum bundle size I prefer the first one and because we based on the standard AbortController API we do not need to polyfill fetch additionally:
Then somewhere in your
index.mjs:Do not forget to exclude transpiled polyfill code from the Babel transpile:
exclude: [ "node_modules/**" ],orexclude: [ "node_modules/abortcontroller-polyfill/**" ],Usage example
P.S. @developit, @prk3, @simonbuerger, @prabirshrestha PR, tests and discussion are welcome 😄