Skip to curriculum
Jump to the editor

14.9. Run tasks in dependency order

Medium
Topics
20–30 min

A page on start-up loads the user, then the user's settings and their team, then a dashboard that needs both. Some steps depend on others; the rest could run side by side. Writing it as one long chain of awaits makes everything wait in line. Better: describe each task and what it needs, and let a runner start every task as soon as its dependencies are done.

Write runTasks(tasks) in run-tasks.js. Each task has a run function and a list of deps. The runner starts a task once all its dependencies have finished, passes it their results, and resolves with every task's result.

Signature

JAVASCRIPT
function runTasks(tasks) // returns a Promise of { name: result }
  • tasks: an object. Each key is a task name; each value is { deps, run }:
    • deps: an array of task names this task needs (may be missing or empty).
    • run(results): does the work. results has one key per dependency, holding its result. It may return a promise or a value, or throw.
  • Returns a promise of an object with every task's result by name.

Requirements

  1. Tasks with no dependencies start at once, all together, unless one of them throws while starting (rule 5): then the ones after it do not start.
  2. A task starts exactly when its last dependency finishes: not in waves or levels, and never before.
  3. run receives an object with exactly its dependencies' results. Each task runs once, even when many tasks depend on it. A name listed twice in deps counts once.
  4. When every task has finished, the promise resolves with all results by name. No tasks resolve with {}.
  5. The first failure (a rejection, or an error run throws) rejects the promise with that error, and no task starts after it. A throw is a failure at the moment it is thrown: tasks that became ready at the same moment and have not started yet do not start.
  6. If a dependency names a task that does not exist, the promise rejects with an Error naming it, before any task runs.
  7. If tasks depend on each other in a cycle (including a task that depends on itself), the promise rejects with an Error whose message contains "cycle" and the names of the tasks on it, before any task runs.
  8. runTasks never throws and does not change tasks.

Examples

JAVASCRIPT
const results = await runTasks({
  user:      { deps: [], run: () => fetchUser() },
  settings:  { deps: ["user"], run: ({ user }) => fetchSettings(user.id) },
  team:      { deps: ["user"], run: ({ user }) => fetchTeam(user.teamId) },
  dashboard: { deps: ["settings", "team"], run: ({ settings, team }) => build(settings, team) },
});
// user first; settings and team side by side; dashboard when both are done
JAVASCRIPT
// fast takes 100 ms, slow takes 300 ms, afterFast needs only fast:
// afterFast starts at 100 ms, while slow is still running
JAVASCRIPT
runTasks({ a: { deps: ["b"], run }, b: { deps: ["a"], run } });
// rejects: "Dependency cycle among: a, b" (your wording may differ); nothing runs

Environment

  • Plain JavaScript in a browser page; the tests call the global runTasks from run-tasks.js.
  • The tests run on a virtual clock that replaces setTimeout; their tasks take a set number of ms and record when they start.
  • Limiting how many tasks run at once is not part of this problem. Listing build stages without running anything is problem 14.12.

Hints

  1. For each task, what single number tells you whether it is ready to start?
  2. A cycle means some tasks can never reach "ready". How can you find that out without running anything?

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…