await...defer in forEach loop await...defer in forEach loop mongoose mongoose

await...defer in forEach loop


Your intuition is incorrect: IcedCoffeeScript's await...defer cannot block a synchronous forEach loop.

Remember that IcedCoffeeScript compiles to JavaScript, which does not support blocking. Once you've called forEach on an array, you've committed to iterating through that entire array before any events can fire.

The good news is that you can get the behavior you want by using IcedCoffeeScript's own loop constructs. In your case,

for info, index in anArray ->  ...

For more information on dealing with JavaScript's non-blocking event model, check out my new book, Async JavaScript.