genki.nakamura

genki.nakamura

Build a Concurrent Task Scheduler

Implement a JavaScript function called runWithConcurrency() that executes asynchronous tasks while limiting the maximum number of tasks running simultaneously.

Requirements:

  1. Accept an array of asynchronous task functions.

  2. Accept a maximum concurrency limit.

  3. Never execute more than the specified number of tasks simultaneously.

  4. Execute all tasks, even if some tasks fail.

  5. Preserve the original task order in the returned results.

  6. Return both successful results and errors.

  7. Handle an empty task array.

    Additional challenges

    • What happens if the concurrency limit is 0?
    • How would you handle a task that never resolves?
    • How would you implement cancellation using AbortController?
    • How would you prevent memory leaks when processing 100,000 tasks?
/js

First Post!

MarcusHale

MarcusHale

Fun one. The version I’d write starts min(limit, tasks.length) workers that each grab the next index from a shared counter, await that task, and write { status, value } or { status, reason } into results[i]. Writing by index keeps the original order for free, and since every worker catches its own errors, one failure never stops the rest. That also covers the 100,000 case. You never build 100k promises up front, only as many as the limit. For a limit of 0 I’d just throw a RangeError. Otherwise you get zero workers and it resolves right away without running anything, which is a nasty surprise. The never-resolving task is the tricky bit. Racing it against a timeout frees the slot, but the task itself keeps running because a promise can’t be cancelled. That’s where AbortController earns its place: pass a signal into each task and abort it on timeout, so tasks that respect the signal actually stop.

Where Next?

Popular Backend topics Top

Rainer
Is there somewhere a good introduction to rust for experienced programmers (with years of C++/C#/Java experience)? Wanted to give it a t...
New
bsek43
Hello everyone, I’ve started learning Elixir and Phoenix few months ago and while I mostly grasped Elixir’s functional concepts and Phoe...
New
Fl4m3Ph03n1x
Background I am trying to encode a structure into json format using the Jason library. However, this is not working as expected. Code L...
New
osbre
Hello everyone I’m trying to implement a “magic link” or “one-time login link” functionality I wonder what a secure way to implement it...
New
MarkIden
Hi, Recommend pls your favorite learning resources in Go, with best books, podcasts etc.
/go
New
sona11
I wrote this code to calculate Fibonacci numbers by specifying the size. The results are correct, however the one thing that concerns me ...
New
pillaiindu
What is the difference between using :references and :belongs_to in the following command? bin/rails generate scaffold LineItem product:...
New
jaeyson
Hi! I have clarifications (please correct me, as I mostly mix/confuse this details) with the following: The term RAG here where it read...
New
yamuna
Hi everyone, I’m trying to run deepseek-coder:6.7b-instruct-q4_K_M in Docker using Ollama to create an LLM that will be used by CrewAI a...
New
Fl4m3Ph03n1x
Background As I often do, I read books to learn and improve myself. I also enjoy teaching and helping others when I can, so this is somet...
New

Other popular topics Top

PragmaticBookshelf
Free and open source software is the default choice for the technologies that run our world, and it’s built and maintained by people like...
New
PragmaticBookshelf
Write Elixir tests that you can be proud of. Dive into Elixir’s test philosophy and gain mastery over the terminology and concepts that u...
New
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
PragmaticBookshelf
Create efficient, elegant software tests in pytest, Python's most powerful testing framework. Brian Okken @brianokken Edited by Kat...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
PragmaticBookshelf
A concise guide to MySQL 9 database administration, covering fundamental concepts, techniques, and best practices. Neil Smyth MySQL...
New