let array = [
new Promise((resolve, reject) =>{
setTimeout(() => resolve(1), 2000) // 2 sec (Promise A)
}),
new Promise((resolve, reject) =>{
setTimeout(() => resolve(2), 3000) // 3 sec (Promise B)
}),
new Promise((resolve, reject) =>{
setTimeout(() => resolve(3), 1000) // 1 sec (Promise C)
}),
new Promise((resolve, reject) =>{
setTimeout(() => resolve(4), 3000) // 3 sec (Promise D)
})
];
I have above given array of promises, How to execute the given array of promises in sequential order, like - Promise A should get resolved before Promise B, C and D. Promise B should get resolved after Promise A and Before Promise B and C.
Promise B should get called only, if Promise A got settled by either success or failure. Promise C should get called only if Promise B gets settled...and so on. And the end it will return [1,2,3,4] as a result.
So, the Total time to execute all the promises from the array will take 2+3+1+3 =9 sec
Promise.sequentialOrderExecution = function (promises) {
// implementation
}
Promise.sequentialOrderExecution.then(result => {
console.log(result) // should print [1,2,3,4]
})
I tried but unable to solve this problem.