node.js resolve promise and return value
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The core rule in Node.js is simple: if a function does asynchronous work, you cannot return its final value synchronously. You must either return a Promise and let the caller await it, or continue the workflow inside a .then() chain. Most confusion comes from trying to assign a future value to a normal variable and use it immediately.
The Wrong Mental Model
This pattern does not work:
The function returns before the Promise settles. result gets assigned later, after the synchronous part has already finished.
Return the Promise Instead
If the function is asynchronous, return the Promise itself.
This is the basic Promise contract: the caller receives a Promise now and the actual value later.
Use async and await
In modern Node.js, async and await are usually the clearest way to express the same idea.
An async function always returns a Promise, even when it looks like it returns a plain value.
Real Example with File I/O
Asynchronous confusion shows up most often with filesystem or network operations.
Here readConfig() does not return the parsed object directly to synchronous code. It returns a Promise that resolves to the parsed object.
Transforming Promise Results
You can return a derived value from a .then() callback, and that derived value becomes the resolution of the next Promise in the chain.
That is how you “return a value from a Promise”: not by breaking out into synchronous code, but by returning from the async continuation.
Returning from Nested Callbacks
Another common bug is returning from inside a .then() and expecting it to return from the outer function.
The return value only returns from the callback passed to .then(). It does not return from broken().
Correct version:
Handling Errors
When you return Promises, error handling has to stay in the async path too.
If you forget to await or attach .catch(), rejections can escape and become unhandled Promise warnings or process failures depending on your runtime and settings.
Multiple Async Values
When you need several async results, combine them instead of forcing them into synchronous variables.
This keeps the whole flow in the Promise model rather than pretending the values already exist.
Common Pitfalls
The most common mistake is trying to return an asynchronously computed value from a synchronous function. Another is believing that return inside .then() returns from the outer function, when it only returns from the callback. Developers also often mix await and plain function calls inconsistently, which makes control flow harder to follow. Finally, forgetting to handle Promise rejection leaves async errors unobserved until runtime.
Summary
- You cannot synchronously return the future result of a Promise.
- Return the Promise itself or make the function
async. - Use
awaitor.then()in the caller to access the resolved value. - A
returninside.then()only affects the Promise chain, not the outer function. - Keep error handling in the async path with
tryandcatchor.catch().
Related reading
- Node.js Streams onend completing before asynchronous onreadable completed
- node.js vs. asp.net async pages
- nodejs wait until all MongoDB calls in loop finish
- Nodejs why is await only restricted to async functions?
- Node.js vs .Net performance
- NodeJS with DynamoDB throws error AttributeValue may not contain an empty string
- Non-blocking asynchronous Jersey JAX-RS with CompletableFuture
- non-blocking io on Windows anonymous pipes
.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.