Skip to curriculum
Jump to the editor

12.1. Run async callbacks in sequence

Medium
Topics
15–25 min

Before promises, asynchronous work in JavaScript reported back through callbacks: you passed a function, and the work called it later with (error, value). Many tasks still have to happen in order: read a config file, then connect with what it says, then load the first page of data. Each step must wait for the one before it, and one failure must stop the rest.

Write runInSequence(tasks, done) in sequence.js. It runs callback-style tasks one after another, collects their values in order, and reports once to done.

Signature

JAVASCRIPT
function runInSequence(tasks, done) // returns undefined
  • tasks: an array of functions. Each has the form task(callback) and calls callback(error, value) when it finishes, usually later from a timer or an event.
  • done: called once at the end, as done(null, results) on success or done(error) on failure.
  • An error is a truthy first argument. null, undefined and false mean success.

Requirements

  1. The first task starts during the call to runInSequence.
  2. Each next task starts only after the previous one called back without an error, and at once when it does.
  3. When every task has succeeded, done(null, results) is called, with results holding each task's value in task order (a task that calls back with no value gives undefined).
  4. At the first error, done(error) is called at once with that error and no results, and no later task starts.
  5. done is called exactly once. If a task calls its callback a second time, the second call is ignored, whatever it holds.
  6. An empty array calls done(null, []).
  7. Tasks may call back synchronously, inside the task(callback) call itself; that must still work, however many tasks there are: 100,000 tasks that all call back synchronously must not run out of call stack.
  8. The tasks array is not changed, and two runs at the same time do not share results.
  9. runInSequence returns undefined: the results go to done.

Examples

JAVASCRIPT
const wait = (ms, value) => (callback) => setTimeout(() => callback(null, value), ms);

runInSequence([wait(300, "config"), wait(100, "connection"), wait(50, "page 1")], (error, results) => {
  console.log(error, results);
});
// 450 ms later: null ["config", "connection", "page 1"]
JAVASCRIPT
const fail = (callback) => setTimeout(() => callback(new Error("disk full")), 20);

runInSequence([wait(10, "a"), fail, wait(10, "never starts")], (error, results) => {
  console.log(error.message, results);
});
// at 30 ms: "disk full" undefined
JAVASCRIPT
runInSequence([], (error, results) => console.log(error, results));
// null []

Environment

  • Plain JavaScript in a browser page; the tests call the global runInSequence from sequence.js.
  • The tests' tasks use setTimeout on a virtual clock, so no test waits for real time.
  • A task that throws instead of calling back is out of scope.
  • Promises and async functions are allowed but not needed.

Hints

  1. A for loop starts every task at once. What could start task i + 1 only when task i is finished?
  2. How can each callback remember whether it has already been called?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…