promise-with-order
Micro module to resolve multiple promises in order.
Lot of time you want to trigger multiple async task together but you want them to get resolved sequentially. PromiseWithOrder wraps passed promise to a promise which resolves in sequential order.
Installation
npm install promise-with-order
Example
; /** * Create a instance of PromiseWithOrder in which all wrapped promise will be resolved sequentially.*/const pwo = ; /* Usage*/ const promise1 = pwo; //asyncTask task return a promise here which resolves after 1s.const promise2 = pwo; //promise which resolves after .5sconst promise3 = pwo; promise1; promise2; promise3; /** log will be Resolved Promise1. Resolved Promise2. Resolved Promise3. Hello world**/
Notice here even the promise resolves in any order, the wrapped promise are always resolved in order.
Promise rejections
By default if any of the promise fails, all the subsequent wrapped promise will fail. This is considering you need resolve to be in order only when a resolution of promise is dependent on previous resolution.
const promise1 = pwo; //asyncTask task return a promise here which resolves after 1s.const promise2 = pwo; const promise3 = pwo; //promise which resolves after .5s promise1; promise2; promise3; /** log will be Resolved Promise1. Promise 2 got rejected because of error in promise 2. Promise 3 got rejected because of error in promise 2.**/
But in case a promise does not have side effect on next promise but still has to be resolved in order. You can pass allowReject
flag.
const promise1 = pwo; //asyncTask task return a promise here which resolves after 1s.const promise2 = pwo; const promise3 = pwo; //promise which resolves after .5s promise1; promise2; promise3; /** log will be Resolved Promise1. Promise 2 got rejected because of error in promise 2. Resolved Promise3..**/
Resetting PromiseWithOrder instance
You can use .reset
method to reset PromiseWithOrder instance.
const promise1 = pwo;const promise2 = pwo; pwo; const promise3 = pwo;const promise4 = pwo;
In this promise1 and promise2 will be resolved in order. Same for the promise3 and promise4. But promise3 and promise 4 is independent of promise1 or promise2.