passing multiple arguments to promise resolution within setTimeout
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When using Promises with setTimeout, developers sometimes try to pass multiple values to resolve as separate arguments. A Promise resolves to one value only, so extra arguments are ignored. The correct pattern is to wrap multiple outputs in one container such as an object or array.
Promise Resolution Rules
The Promise resolve function accepts a single value. That value can be a primitive, object, array, or another promise.
If you do this:
only the first value is used. This often causes subtle bugs when callers expect all values.
Correct Pattern: Resolve an Object
Objects are usually the clearest option because keys describe meaning.
This keeps caller code readable and resilient to future field additions.
Alternative Pattern: Resolve an Array Tuple
If order is stable and compactness matters, return an array and destructure it.
This works well, but object returns are usually easier for long-term maintenance.
Handling Success and Failure with Structured Data
For robust async APIs, return a predictable shape and reject errors explicitly.
Keeping structured success payloads and explicit rejection paths reduces caller ambiguity.
Timeouts with External Data and Cancellation
In real applications, setTimeout wrappers are often used for simulated latency or timeout protection. If function is cancellable, design API to communicate cancellation state consistently.
A simple pattern is to resolve object with cancelled flag when cancel path is expected, and reserve rejection for true errors.
This avoids treating cancellation as exceptional failure in caller logic.
Testing Multi-Value Promise APIs
Unit tests should validate exact payload shape, not only truthy values.
Example assertions to include:
- resolved type is object or array as designed
- required fields exist
- timeout path and reject path behave consistently
If team uses TypeScript, define return types explicitly to prevent mismatch between implementation and caller expectations.
TypeScript Contract Example
When using TypeScript, returning a named payload interface helps callers avoid positional mistakes and keeps refactors safe.
Common Pitfalls
- Passing multiple positional arguments to
resolveand expecting all of them. - Returning ambiguous arrays without clear ordering contract.
- Mixing cancellation, success, and error into inconsistent payload shapes.
- Rejecting expected business states that should be normal resolved outcomes.
- Skipping tests for delayed timing and failure branch behavior.
Summary
- Promise resolution returns one value, not multiple separate arguments.
- Wrap multiple outputs in an object or array.
- Prefer object payloads for readability and future compatibility.
- Keep success, failure, and cancellation contracts explicit.
- Test payload shape and branch behavior to avoid async integration bugs.
Related reading
- Passing object by reference to stdthread in C11
- passing parameters to Node.js async waterfall
- Passing value into next Promises argument
- Pattern for updating slave SQL Server 2008 databases from a master whilst minimising disruption
- Perform actions as promises get fulfilled using Promise.all
- Performance of nodejs async hooks
- Patterns for Multithreaded Network Server in C
- Patterns/Principles for thread-safe queues and master/worker program in Java
.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.