lostyzd
lostyzd

Reputation: 4523

How to chain multiple tasks with javascript?

I have some async tasks to do, like task1, task2, task3, ..., and their relationships is like the graph below.

enter image description here

This is a directed acyclic graph so I can use topological sorting to figure out one possible execution route.

But it is still non trivial task for human, maybe like this.

task7.start().done(check_if_5_is_done_then_start_11, check_if_3_is_done_then_start_8);
task5.start().done(check_if_7_is_done_then_start_11);
task3.start().done(check_if_7_is_doen_then_start_8, check_if_11_is_doen_then_start_10);

....

I'd prefer a more elegant solution, like:

var Tasks = new Tasks({
  task7:  [func7,  []],
  task11: [func11, ["task7", "task5"]],
  task5:  [func5, []],
  task8:  [func8, ["task3"]]
  ...
});

Tasks.start().alldone(function () { // done  });

Upvotes: 2

Views: 602

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146014

Looks like pretty much exactly the use case async.auto is designed for.

Upvotes: 2

Related Questions