Last active
June 1, 2022 03:18
-
-
Save cgiosy/14b8a0fd425bb47c1ccc5697d8c36ecc to your computer and use it in GitHub Desktop.
Asynchronous Function Concurrent Executor
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 concurrent = async(limit, fns) => { | |
const result = []; | |
const runningTasks = []; | |
for (const fn of fns) { | |
const promise = fn(); | |
result.push(promise); | |
const emptyIndex = runningTasks.length === limit ? await Promise.race(runningTasks) : runningTasks.length; | |
const currentIndex = emptyIndex; | |
runningTasks[emptyIndex] = promise.then(() => currentIndex); | |
} | |
return await Promise.all(result); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: 각 task의 weight가 1로 한정되지 않고, 사용자가 넣을 수 있게 하기