user3338991
user3338991

Reputation: 461

js await loop with promises to finish

let array = []
for (let n = 0; n < 1000; n++){
   array.push(somePromise)
}
console.log(array.length)

How to modify the code, so that in line where I have console.log will be executed only after the array is filled. Filling might be async, but I am interrested in sync version as well.

Upvotes: 1

Views: 39

Answers (1)

Prime
Prime

Reputation: 2849

Actually, filling is sync. Do you mean Promise.all()? Using Promise.all, you can handle some after all promises are completed.

let array = []
for (let n = 0; n < 1000; n++){
   array.push(somePromise)
}
Promise.all(array)
.then((res) => {
   // You can handle some here
   console.log(res);
})

Upvotes: 1

Related Questions