Created
May 31, 2023 05:34
-
-
Save retorquere/4955b69c744ca4d779db90f7f207cfa3 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(); | |
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