Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created May 31, 2023 05:31
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/b95948dd4000ee15d05e33cad604989b to your computer and use it in GitHub Desktop.
Save retorquere/b95948dd4000ee15d05e33cad604989b 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();
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];
for (const dep of task.dependency) run(dep);
task.job();
done.set(name, true)
}
for (const name in tasks) run(name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment