Sitemap

Promises: forEach, reduce, and task queues

1 min readMar 13, 2021

No matter how hard they tried, they couldn’t coax forEach to use await — at least not as expected. Instead of resorting to for .. in (to which Airbnb’s linter vociferously disapproved) they discovered a magic incantation involving reduce. Alas! “But how much memory does this consume?” they pondered. “Is this efficient? Has anyone published an npm module that runs a maximum number of tasks in parallel? A task queue of some sort?”🤔

Answers to the questions:

  1. This consumes O(pn) bytes of memory where n is the size of the array a p is the size of a promise, regardless of how the Promises settle
  2. This is inefficient not only in terms of memory but also CPU-wise because of the memory allocations that are eventually cleaned up by the garbage collector. Creating the Promise chain blocks other tasks from running, so this can cause the nodeJS server or the main browser thread to temporarily lock up. Isn’t cooperative multitasking fun?
  3. @goodware and others publish asynchronous task queues on npm. However, in order to use them efficiently, you will need to use a for loop of some kind. The Array functions forEach() and reduce() are not helpful because they don’t await Promises returned by callbacks since they are synchronous functions.

--

--

Terris Linenbach
Terris Linenbach

Written by Terris Linenbach

Coder since 1980. Always seeking the Best Way. CV: https://terris.com/cv

No responses yet