271 milliseconds. That is how long my batch lived before task 4 threw, and it is the only number in this piece that came out the same no matter what I did to the results array. Everything else about the abort changed depending on one small decision made before the first worker started: whether the results array was full of holes, or full of slots marked skipped.

johnnylemonny asked about this in the comments on his Promise.all piece, and his question was sharper than most of the replies it got. In a fail-fast mapWithConcurrency, rethrowing inside the worker makes the whole call reject, so the results array is never returned and the slots that never started stay uninitialized holes. His fix: fill the array with { status: "skipped" } before starting the workers, so the caller can tell the tasks that failed apart from the ones that never ran. I ran his shape twice, both ways, and he is right about the shape and wrong about one slot. The pre-fill fixes the array. It does not fix what the array says about the task that threw.

The setup was small on purpose. Ten tasks, three workers, one throw. Each task waits a set time and then returns { status: "ok", value: i * 10 }, except task 4, which throws after 150 milliseconds. The delays were picked so the throw lands mid-batch: tasks 0, 1 and 2 start first, task 4 starts once task 0 finishes, and it throws at 270 milliseconds, with five tasks still waiting or running. Both arms ran twice, and both repeats came back identical down to the tenth of a millisecond, so the numbers below are the run, not an average of two different ones.

async function mapWithConcurrency(tasks, limit, prefill) { const results = prefill ? tasks.map(() => ({ status: "skipped" })) : new Array(tasks.length); let next = 0; async function worker() { while (next < tasks.length) { const i = next++; results[i] = await tasks[i](); } } await Promise.all(Array.from({ length: limit }, () => worker())); return results; } 
Holes Pre-filled skipped
Abort wall time 271.1 ms 270.2 ms
Slots finished ok 3 of 10 3 of 10
Slots the caller can read 3 of 10 10 of 10
.map callback runs 3 of 10 10 of 10
results[5].status throws TypeError "skipped"
After a JSON round-trip, slots 4 and 5 both null both {"status":"skipped"}

What the caller actually gets from holes

The holes arm is worse than it looks in the code. results.length says 10, which is a lie in the only sense that matters: only 3 keys exist in the array. The other 7 slots are not undefined values. They are absence. Reading one gives you undefined, but asking whether it is there gives you false, and .map never even visits them: the callback ran 3 times out of 10. A consumer that counts finished work with .filter gets 3, which happens to be right, and a consumer that builds a report with .map gets a report with 3 entries and no complaint, which is wrong in a way nothing flags.

First, results[5].status throws a TypeError, because there is nothing at slot 5 to read a status from. Any consumer that reads a field off every slot crashes on the first hole it meets. Second, the holes do not survive a round-trip. JSON.stringify turns each hole into null, and after parsing, slot 4, the task that threw, and slot 5, a task that never started, are both null. A real null result, a failed task and a never-started task are now the same value, and no amount of reading the parsed data can separate them again. If the results ever cross a network boundary, get written to a file, or get logged as JSON, which is to say if they ever leave the function, the distinction johnnylemonny wanted is already gone.

The pre-fill fixes the shape, then lies about one slot

The pre-filled arm reads like the answer. Ten keys, ten callbacks, no TypeError, no nulls after a round-trip. Slot 5 reads {"status":"skipped"}, which is true in the only sense the caller cares about: nothing ran there, nothing will, and the caller can retry it or report it without guessing. That is a real improvement, and it costs nothing measurable: the abort took 270.2 milliseconds against 271.1, a 0.9-millisecond gap across two identical repeats, which is timer noise, not a price.

But read slot 4. Task 4 is the one that threw. In the pre-filled array it reads {"status":"skipped"}, exactly like slot 5, which never started. It was not skipped. It ran, it hit a bad input row, and it failed. The pre-fill cannot tell those apart, because nothing ever wrote the failure down: the worker threw, the throw jumped out of the worker, and the slot kept whatever it was filled with at the start. Two more slots lie the same way. Slots 3 and 5 were still running when the abort landed at 271 milliseconds, their work abandoned halfway, and both read "skipped" too. Out of the seven slots marked skipped, only four never started. Two were mid-flight and one had failed, and the array calls all seven the same thing.

The fix for that is one line inside the worker, and it belongs in the same place as the pre-fill, not after it:

async function worker() { while (next < tasks.length) { const i = next++; try { results[i] = await tasks[i](); } catch (err) { results[i] = { status: "failed", error: err.message }; throw err; } } } 

With that, the failed slot reads failed, the never-started slots read skipped, and the in-flight ones still read skipped, which is the one remaining blur: a task that was running when everything stopped looks the same as a task that never began. Closing that last gap takes a "running" mark when the worker picks a task up, which is a second line and worth it only if your caller retries in-flight work differently from untouched work. For most callers, failed versus skipped is the distinction that decides the retry, and the pre-fill alone does not give it to you.

The part nobody mentions: the plain call hands you nothing

There is a catch underneath both arms, and it is the one that decides whether any of this matters. In the plain shape, Promise.all rejecting means the caller gets the error and nothing else. The results array is gone with the rejection. My run kept the array alive in a variable outside the call, which is how I could inspect it at all. A caller that just awaits the function gets an error object, and whether the array was holes or pre-filled skipped makes no difference to them, because they never see it.

So the pre-fill is half a pattern. The other half is keeping the array reachable after the rejection: attach it to the error, return a partial result alongside it, or shape the call so the array comes back with the failure instead of being thrown away with it. Without that half, you have built a careful label for a box the caller never receives. With it, the pre-fill plus the failed-mark in the worker gives the caller everything it needs: three finished, one failed with its message attached, six never started, and no slot that has to be guessed at.

That is where I land. Pre-fill the slots. It is one line, it costs nothing, and it turns an array the caller cannot read into one it can. Just do not stop there, and do not believe the labels it writes. Mark the failure in the worker before you rethrow, and make sure the array survives the rejection, or the pre-fill is a tidy label on a box nobody opens.

Have you hit this one in a real batch? What did your caller get back when the middle of the batch threw, and did it know which tasks were safe to retry?

P.S. Everything I write stays free — that part doesn't change. But if this one was worth something to you, you can buy me a coffee ☕ (one-time or monthly, both work). It covers the API credits and hosting behind these experiments — and honestly, it's also the nudge that gets the next one started on the evenings I'd otherwise skip it. — Salam

Buy me a coffee

AheadKit on Gumroad — Beginner-Friendly Books & Packs