How it works...

All 10 workers are instructed to post a message after their given timeout but, as you can see, only a single worker actually posts a message back to the main thread. This is because, after this first worker posts, we call terminate on all of the workers. This means that they will stop immediately, and unless another worker has already posted, it never will. So, we will almost always see a single message posted. It is possible that one or two other workers will post before being terminated.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.