Sikau

Advanced JavaScript · Day 22 · about 35 min

Project: Task queue

Run many jobs, a few at a time.

Not all at once, not one by one

Uploading 50 photos: one at a time is slow, all 50 at once overloads the network. The sweet spot is a concurrency limit — say, 3 at a time. You'll build the helper real apps use for this.

The plan

  • Keep an index of the next task to start, and a results array.
  • Start limit "workers". Each worker loops: take the next task, await it, store its result by index.
  • When there are no tasks left, the worker stops.
  • await all workers with Promise.all, then return results (in the original order).

The rest of this lesson

  • The worker patternexplain
  • Why it's safe and fastexplain
  • How long?predict
  • A worker's looporder
  • Build runWithLimitchallenge
  • Badge quizquiz
  • Unit 4 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 27 lessons of Advanced JavaScript