Created
May 31, 2023 05:31
-
-
Save retorquere/b95948dd4000ee15d05e33cad604989b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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