Skip to content

Instantly share code, notes, and snippets.

@cgiosy
Last active June 1, 2022 03:18
Show Gist options
  • Save cgiosy/14b8a0fd425bb47c1ccc5697d8c36ecc to your computer and use it in GitHub Desktop.
Save cgiosy/14b8a0fd425bb47c1ccc5697d8c36ecc to your computer and use it in GitHub Desktop.
Asynchronous Function Concurrent Executor
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);
};
@cgiosy
Copy link
Author

cgiosy commented Feb 21, 2022

TODO: 각 task의 weight가 1로 한정되지 않고, 사용자가 넣을 수 있게 하기

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment