admin管理员组文章数量:1391947
I have series of promise chains, which took sufficient time to get pleted. Below is the sample chain setup:
myJob1()
.then(myJob2)
.then(myJob3)
.then(myJob4)
.then(myJob5)
.then(myJob6)
.catch(myJobError);
In mean time when this job is running, if the person on UI think to cancel it, How can it be cancelled in whatever stage/function execution it is?
What can be the possible solution?
I have series of promise chains, which took sufficient time to get pleted. Below is the sample chain setup:
myJob1()
.then(myJob2)
.then(myJob3)
.then(myJob4)
.then(myJob5)
.then(myJob6)
.catch(myJobError);
In mean time when this job is running, if the person on UI think to cancel it, How can it be cancelled in whatever stage/function execution it is?
What can be the possible solution?
Share Improve this question edited Aug 21, 2017 at 13:52 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Aug 21, 2017 at 11:55 Manish KumarManish Kumar 1,17616 silver badges29 bronze badges 3- Really need more info, eg. especially when you say client. Does your client have some form of RPC in place?. Cancelling a promise chain would be easy, but it's getting the mand to the back end that's the tricky part. – Keith Commented Aug 21, 2017 at 12:03
- 1 Possible duplicate of How to cancel an EMCAScript6 (vanilla JavaScript) promise chain – TheCog19 Commented Aug 21, 2017 at 12:04
-
You can't cancel, but you can do a reject. Which would then stop the rest of the then's from being called. You would have to test for something in each job, and if that test passed you would return a new rejected promise, ie
return Promise.reject('reason')
– Patrick Evans Commented Aug 21, 2017 at 12:05
2 Answers
Reset to default 4One alternative to modifying code for multiple job functions might be to check a user cancelled flag between jobs. If the granularity of this kind of checking is not too course, then you could asynchronously set a (somewhat) global cancelled flag and proceed along the lines of:
let userCancelled = false;
let checkCancel = function( data) {
if( userCancelled)
throw new Error( "cancelled by user"); // invoke catch handling
return data; // pass through the data
}
myJob1()
.then(myJob2).then( checkCancel)
.then(myJob3).then( checkCancel)
.then(myJob4).then( checkCancel)
.then(myJob5).then( checkCancel)
.then(myJob6).then( checkCancel)
.catch(myJobError);
Don't forget that if you do check the cancelled flag inside a job, all you need to do is throw an error to have it bubble down the promise chain.
There is no way to cancel the promise (remember each of thens is returning a new promise) or clear the then
callback.
Probably you are looking for something like redux-observable
, where you can specify clause, until promise execution is actual.
See more in details: https://github./redux-observable/redux-observable/blob/master/docs/recipes/Cancellation.md
As alternative I may only suggest you to create and manage some flag which determines whether further process is needed or not:
// Inside each of promises in chain
if (notCancelled) {
callAjax(params).then(resolve);
}
Or reject:
// Inside each of promises in chain
if (cancelled) {
// Will stop execution of promise chain
return reject(new Error('Cancelled by user'));
}
本文标签: javascriptNodeJSKill promise chain if event receivedStack Overflow
版权声明:本文标题:javascript - NodeJS - Kill promise chain if event received - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744707799a2620944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论