node.js async.series not working
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
async.series from the async library runs an array of functions sequentially, passing results to a final callback. The most common reason it "does not work" is forgetting to call the callback parameter inside each function. If the callback is never called, async.series hangs and the final callback never fires. Other causes include passing the wrong callback signature, throwing errors instead of passing them to the callback, or mixing up async.series with async.parallel.
How async.series Works
Each function receives a callback(err, result) parameter. Call it with null as the first argument on success, or an Error on failure. If any task passes an error, the remaining tasks are skipped and the final callback fires immediately with that error.
Problem 1: Not Calling the Callback
Fix: Always call callback() in every code path:
Problem 2: Calling Callback Multiple Times
Fix: Return after calling the callback, or use if/else:
Problem 3: Throwing Instead of Passing Errors
Fix: Wrap in try/catch and pass the error to the callback:
Problem 4: Using async.series With Promises
async.series is callback-based. If your functions return Promises, they will not work correctly:
Fix: Either use the callback parameter or switch to native Promise.all / for...of:
Problem 5: Wrong Callback Signature
Fix: Always pass null as the first argument on success:
Modern Alternative: Native async/await
For new code, async/await replaces async.series entirely:
This is simpler, requires no external library, and handles errors naturally with try/catch.
Common Pitfalls
- Forgetting to call callback: The single most common issue. Every function in the series array must call
callback()in every code path, including error paths and conditional branches. - Mixing async.series and async.parallel:
async.parallelruns all tasks concurrently,async.seriesruns them sequentially. Using the wrong one produces unexpected timing behavior. - Not installing the async library:
async.seriesrequiresnpm install async. The built-in Node.jsasync/awaitis different from theasyncnpm package. - Callback called after return in conditional: If an early
ifbranch callscallback()withoutreturn, the code continues and callscallback()again at the end of the function. - Error handling: If one task passes an error to the callback, all remaining tasks are skipped. Make sure your final callback handles errors:
if (err) { console.error(err); return; }.
Summary
async.seriesrequires callingcallback(null, result)in every function — forgetting this causes silent hangs- Pass errors as the first callback argument:
callback(err), not by throwing - Never call the callback more than once — use
return callback(err)to prevent double calls - For new projects, use native
async/awaitinstead of theasyncnpm library - The callback signature is always
callback(error, result)—nullfirst argument means success
Related reading
- NodeJS async.Whilst
- Node.js async.whilst is not executing at all
- NodeJS Express Async Not Handling More Requests
- node.js How asynchronous are Async loops really?
- node.js AWS dynamodb updateItem
- Nodejs AWS SDK S3 Generate Presigned URL
- NodeJS Background jobs never execute, loop forever
- Node.js Best Practice Exception Handling
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.