Async Api Call Inside Foreach Loop
I ran into a problem that I cannot seem to solve, I'm guessing I'm missing some points in terms of async behaviour. The task is relatively simple: A wallet with money in different
Solution 1:
The problem is that async methods returns Promise and you need to wait them to resolve. The common pattern to do this is next:
Promise.all(arr.map(v =>someAsyncFunc(v))).then((resolvedValues) => {
resolvedValues.forEach((value) => {
// Do your stuff here
});
});
So basically you are initializing bunch of async calls, waiting for them to resolve with Promise.all
and only after that preform some operations on resolved data.
Post a Comment for "Async Api Call Inside Foreach Loop"