I have a list of tasks and all these tasks need to be executed only after all the dependencies are resolved for each task. I am struggling to figure out a way to finish running all the tasks in optimal time.
// Each node is a async job, illustrated by setTimeout.
// A and C can run at the same time.
// D, needs to wait for A and C to be done.
// E needs to wait for A and D to be done.
function runTasks(tasks) {
// run tasks
}
// Sample of tasks
var tasks = {
'a': {
job: function (finish) {
setTimeout(function () {
console.log('a done');
finish();
}, 500);
},
},
'c': {
job: function (finish) {
setTimeout(function () {
console.log('c done');
finish();
}, 200);
},
dependencies: [],
},
'd': {
job: function (finish) {
setTimeout(function () {
console.log('d done');
finish();
}, 100);
},
dependencies: ['a','c'],
},
'e': {
job: function (finish) {
setTimeout(function () {
console.log('e done');
finish();
}, 200);
},
dependencies: ['a', 'd'],
},
};