Abortable fetch with timeout support
npm install cp-fetch!Travis (.com)

!npm
!npm bundle size
!David
cpFetch is a simple wrapper around fetch with cancelable promise (c-promise2).
This lib can be used for both backend and frontend development, platform specific fetch API is provided by
cross-fetch package.
``bash`
$ npm install cp-fetch
`bash`
$ yarn add cp-fetch
``javascript
// cross-platform version
const cpFetch= require('cpFetch');
// version that uses the global fetch API, instead of cross-fetch ponyfill
// (for modern browsers only or in case you're using third-party polyfill)
import cpFetch from "cp-fetch/lib/native";
``
#### CDN bundle
Ready for use prebuilt UMD bundles for browser with all dependencies inside.
- production UMD cross-platform bundle with fetch ponyfill (minified ~32KB)
- production UMD bundle (minified ~25KB)
These module bundles are only suitable to load as a script directly from the html page.
If you're using some module bunlder like webpack or rollup,
please import cjs module to avoid potential duplications in your project dependencies tree.
Global module export is cpFetch.
#### Live Example
#### Abortable fetch with timeout
A simple example:
``javascript
const cpFetch= require('cp-fetch');
const url= 'https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s';
const chain = cpFetch(url, {timeout: 10000})
.then(response => response.json())
.then(data => console.log(Done: , data), err => console.log(Error: , err))
setTimeout(()=> chain.cancel(), 1000); // abort the request after 1000ms
// you able to call cancel() at any time to cancel the entire chain at any stage
// Take into account the related network request will also be aborted
``
The same using generators as async function:
``javascript
const cpFetch= require('cp-fetch');
const CPromise= require('c-promise2');
const url= 'https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s';
const chain= CPromise.from(function*(){
try{
const response= yield cpFetch(url, {timeout: 5000});
console.log(Done: , yield response.json())Error:
}catch(err){
console.log(, err)
}
});
setTimeout(()=> chain.cancel(), 1000); // abort the request after 1000ms
``
#### Abortable concurrent requests
``javascript
const cpFetch= require('cp-fetch');
const CPromise = require('c-promise2');
const chain= CPromise.race([
cpFetch("https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=3s"),
cpFetch("https://run.mocky.io/v3/30a97b24-ed0e-46e8-9f78-8f954aead2f8?mocky-delay=5s")
]).timeout(10000).then((response)=> {
console.log(Result :, response.data);We got an error: ${err}
}, function (err) {
console.warn();
});
// the slower request will be aborted
// setTimeout(()=> chain.cancel(), 1000); // abort the request after 1000ms
``
The package exports a wrapped version of the fetch function.
cFetch(url, {timeout, ...nativeFetchOptions}): CPromise
Options:
timeout- the timeout before the promise and related request will be rejected/aborted.
...nativeFetchOptions`- other native options of the fetch function.
Learn more about CPromise features
- cp-axios - a simple axios wrapper that provides an advanced cancellation api
- c-promise2 - promise with cancellation and progress capturing support
- use-async-effect2 - cancel async code in functional React components
- cp-koa - Koa with cancelable middlewares
The MIT License Copyright (c) 2020 Dmitriy Mozgovoy robotshara@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.