Are loops synchronous or asynchronous in JavaScript? (for, while, etc)
Supposing I have:
for(let i=0; i<10; i++){
// A (nested stuff...)
}
//
If you place asynchronous loops inside a for...loop
and want to stop the loop until each operation ends, you must use the async/await
syntax like this.
async function foo() {
var array = [/* some data that will be used async*/]
//This loop will wait for each next() to pass the next iteration
for (var i = 0; i < array.length; i++) {
await new Promise(next=> {
someAsyncTask(array[i], function(err, data){
/*.... code here and when you finish...*/
next()
})
})
}
}
foo().then(() => { /*After foo execution*/ })