Look what happens when combining an async function with a for loop

Take a look at this interesting results of running an async process.

So using an async function in a for loop results in a curroted i. My question is: What solutions can be used?

There’s a good explanation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
(section: Creating closures in loops: A common mistake)

One solution would be to use ES6 let instead of var, e.g.
for (let i in channelsData)
Another solution is to wrap the body of the loop in a self invoking function.

1 Like