I have an array of promise objects that must be resolved in the same sequence in which they are listed in the array, i.e. we cannot attempt resolving an element till the pre
I suppose two approaches for handling this question:
let allPromiseAsync = (...PromisesList) => { return new Promise(async resolve => { let output = [] for (let promise of PromisesList) { output.push(await promise.then(async resolvedData => await resolvedData)) if (output.length === PromisesList.length) resolve(output) } }) } const prm1= Promise.resolve('first'); const prm2= new Promise((resolve, reject) => setTimeout(resolve, 2000, 'second')); const prm3= Promise.resolve('third'); allPromiseAsync(prm1, prm2, prm3) .then(resolvedData => { console.log(resolvedData) // ['first', 'second', 'third'] });
(async () => { const promise1 = new Promise(resolve => { setTimeout(() => { console.log('first');console.log(new Date());resolve() }, 1000) }) const promise2 = new Promise(resolve => { setTimeout(() => {console.log('second');console.log(new Date()); resolve() }, 3000) }) const promise3 = new Promise(resolve => { setTimeout(() => { console.log('third');console.log(new Date()); resolve() }, 7000) }) const promises = [promise1, promise2, promise3] await Promise.all(promises) console.log('This line is shown after 7000ms') })()