I know that promis in js. Are not cancellable but i know genius mind all around the globe must have found some way to make it possible?
comment your answers and let me know...
importCPromisefrom"c-promise2";functionfetchWithTimeout(url,{timeout,...fetchOptions}={}){returnnewCPromise((resolve,reject,{signal})=>{fetch(url,{...fetchOptions,signal}).then(resolve,reject)},timeout)}constpromise=fetchWithTimeout('http://localhost/',{timeout:5000}).then(response=>response.json()).then(data=>console.log(`Done: `,data),err=>console.log(`Error: `,err))setTimeout(()=>promise.cancel(),1000);// cancel the pending chain when you need// yes, the related network request will be canceled too// CPromise.race([// fetchWithTimeout(url1), fetchWithTimeout(url2), fetchWithTimeout(url3)// ]).then(console.log); // The other two slower network requests will be aborted as well
exportclassAsyncComponentextendsReact.Component{state={};asynccomponentDidMount(){console.log("mounted");this.controller=newCPromise.AbortController();try{awaitthis.myAsyncTask1();constresult=awaitthis.myAsyncTask2();this.setState({text:result});}catch(err){if(CPromise.isCanceledError(err)){console.log("tasks terminated");return;}throwerr;}}myAsyncTask1(){returnCPromise.from(function*(){yield...yield...}).listen(this.controller.signal);}myAsyncTask2(){returnnewCPromise((resolve,reject,{onCancel})=>{//code}).listen(this.controller.signal);}componentWillUnmount(){console.log("unmounted");this.controller.abort();// kill all tasks}}
Promise cannot be cancelled, it is the process that returns promise must be cancellable.
For example, XmlHttpRequest is cancellable as it has an abort method.
Fetch api also supports AbortController.signal, that is cancellable.
As Promises are deeply nested, and promise is a result, it cannot be cancelled and even if somehow you ignore by putting timeout, underlying process will not be cancelled.
Hey !! I am a Enthusiast Web developer on my way to explore all inches of web and connect with people. Dont hesitate to reach out to me.
Linkedin: https://in.linkedin.com/in/rautrohit
Hi akash i know we cannot cancel the network call but can u tell me how to not perform the action which was suppose to be performed when promise would have resolved?
Think I’ve only seen cancel by timeout with Promise.race[mainPromise, timeoutPromise].
TimeoutPromise throws after a certain amount of time triggering the catch handler of race.
Full-stack engineer building products, backend systems, and integrations.
Currently working on EV charging infrastructure — OCPP, cloud services, APIs, and systems that connect chargers, platforms, a
Top comments (5)
Using 'c-promise2' package we can do the following trick:
(Live Demo - jsfiddle.net/DigitalBrain/c6njyrt9... )
Simple React Demo of using AbortController signal - codesandbox.io/s/react-fetch-class...
Simple React Demo using hooks - codesandbox.io/s/react-cpromise-fe...
Promise cannot be cancelled, it is the process that returns promise must be cancellable.
For example, XmlHttpRequest is cancellable as it has an abort method.
Fetch api also supports AbortController.signal, that is cancellable.
As Promises are deeply nested, and promise is a result, it cannot be cancelled and even if somehow you ignore by putting timeout, underlying process will not be cancelled.
Hi akash i know we cannot cancel the network call but can u tell me how to not perform the action which was suppose to be performed when promise would have resolved?
Think I’ve only seen cancel by timeout with Promise.race[mainPromise, timeoutPromise].
TimeoutPromise throws after a certain amount of time triggering the catch handler of race.
I think to handle a cancellable request you can use RxJS