If there is no asynchronous code inside the forEach
, forEach
is not asynchronous, for example in this code:
array.forEach(function(item){
//iterate on something
});
alert("Foreach DONE !");
you will see the alert after forEach
finished.
Otherwise (You have something asynchronous inside), you can wrap the forEach
loop in a Promise:
var bar = new Promise((resolve, reject) => {
foo.forEach((value, index, array) => {
console.log(value);
if (index === array.length -1) resolve();
});
});
bar.then(() => {
console.log('All done!');
});
Credit: @rolando-benjamin-vaz-ferreira
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…