Skip to content
Advertisement

Loop asynchronosly through array

I have following code:

JavaScript

As you can see this does not work. So the content of the for loop works pretty well. It goes through the array and works well, but the for loop itself is the problem. How can I get the for loop wait for the inner content to finish before going to the next key?
The timeline should be as followed: key(slot1) --> .reduce stuff --> key(slot2) --> .reduce stuff --> key(slot3) --> .reduce stuff --> fire finish
I experimented with many different structures and can’t find a solution.

ADDITION

JavaScript

Thanks in advance,
filip.

Advertisement

Answer

Your .reduce strategy for the inner loop is good but the outer loop is still synchronous, and starts a promise chain for each key all at the same time. Apply the same thing to the outer loop:

JavaScript

Or with async/await it becomes a friendlier loop:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement