Loading...
users.values().forEach(async (user) => {
await save(user);
});
console.log('All saved');for (const user of users.values()) {
await save(user);
}
console.log('All saved');forEach() does not wait for async callbacks; a for...of loop with await saves each user before logging.
.map() after forEach() and getting a TypeError.forEach() is the last call in a chain, because it returns undefined.forEach() and for...of?Both read every value. for...of supports break, continue and await, while forEach() always runs to the end and ignores returned promises.
forEach() early?Only by throwing. Use some(), find() or a for...of loop with break when you need to stop.