In a Node app, I need to iterate through some items in a synchronous fashion, but some of the operations inside the loop are asynchronous. My code right now looks like so:
someAPIpromise().then((items) => {
items.forEach((item) => {
Promise.all[myPromiseA(item), myPromiseB(item)]).then(() => {
doSomethingSynchronouslyThatTakesAWhile();
});
}
}
This works wonders when the items
is an array of 1. But, once there's more than one item, promise.all()
will just fire off instantly for every item in the array, without waiting for the operation in the loop to end.
All that to say... how can I ensure that the entire operation for each item in the array is run synchronously (even if some operations are async and return a promise)?
Thanks so much!
N