Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created May 31, 2023 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retorquere/4955b69c744ca4d779db90f7f207cfa3 to your computer and use it in GitHub Desktop.
Save retorquere/4955b69c744ca4d779db90f7f207cfa3 to your computer and use it in GitHub Desktop.
const tasks={
a:{
job:function(){console.log("Tasks A running")},
dependency:["c","b"]
},
b:{
job:function(){console.log("Tasks B running")},
dependency:[]
},
c:{
job:function(){console.log("Tasks C running")},
dependency:["b"]
}
};
const done = new Map();
async function run(name) {
switch (done.get(name)) {
case true: return
case false: throw new Error(`loop visiting ${name}`)
}
done.set(name, false)
const task = tasks[name];
await Promise.all(Object.keys(tasks).map(name => run(name)))
await task.job();
done.set(name, true)
}
Promise.all(Object.keys(tasks).map(name => run(name))).then(() => console.log('done'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment