javascript - NodeJS - Kill promise chain if event received -


i have series of promise chains, took sufficient time completed. below sample chain setup:

myjob1() .then(myjob2) .then(myjob3) .then(myjob4) .then(myjob5) .then(myjob6) .catch(myjoberror); 

in mean time when job running, if person on ui think cancel it, how can cancelled in whatever stage/function execution is?

what can possible solution?

one alternative modifying code multiple job functions might check user cancelled flag between jobs. if granularity of kind of checking not course, asynchronously set (somewhat) global cancelled flag , proceed along lines of:

let usercancelled = false; let checkcancel = function( data) {     if( usercancelled)         throw new error( "cancelled user"); // invoke catch handling     return data; // pass through 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 if check cancelled flag inside job, need throw error have bubble down promise chain.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -