

The following code will print Processed 1 and then hang in the endless loop. I recommend you to read Concurrency model and Event Loop on MDN, which explains it in more detail and formally better than I can off the top of my head :) The effects of the execution model Endless loops cannot be avoided to the caller of iterator.next() when the generator function just yielded a value.

But even then no parallelism is created, the VM just switches the current queue item it is processing, e.g. The only way your code can be interfered with (preempted) is using these statements. It runs every dequeued element either to completion, to a yield (in generator functions), an await (in async functions) or an iterator.next() (on a generator function). Simply speaking, the VM executing the code has a queue from which it dequeues an action (e.g. This is not quite right since JavaScript (i.e. My understanding is that the first one is parallel because the await applies to each invocation of the async lambda that is passed to map. make sure nothing is already defined on these members I based these implementations on the answer found at this SO question: = async function (Īwait Promise.all(this.map(async (item: any) => await func(item))) Ī = async function (Įxample usage: await someList.forEachParallel(item => this.SomeAsyncOperation(item)) Ī little additional code is: // type declarationįorEachParallel(func: (item: T) => Promise): Promise įorEachSequential(func: (item: T) => Promise): Promise In contrast, the await in the sequential function applies to the outer function, so each iteration of the for loop will block, and the next iteration will only occur once the previous one completes. So each invocation will block asynchronously and run in parallel. Are these good implementations of parallel and sequential "extension" methods, and do they accomplish what I think they do?
