You can’t get a timer callback’s return value from setTimeout(). setTimeout() returns a timer identifier immediately; to use a value computed later, pass it to a callback or resolve a Promise and consume it with await or .then().
Why a callback’s return value is not available
setTimeout() schedules a function to run later, then returns without waiting for it. MDN describes it as an asynchronous function that “returns immediately after scheduling the callback function or code to run.” Its return value is a timer identifier, not the result of the callback.
const timerId = setTimeout(() => {
return getValue(); // returned only from this callback
}, 1000);
console.log(timerId); // timer identifier, not getValue()'s result
The callback’s return belongs to that later function call. It does not travel back to the code that scheduled the timer. Likewise, return setTimeout(...) returns the timer identifier.
Reference: MDN Web Docs: Window: setTimeout() method.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11#1 Best Overall
Pass the result to a callback
If the surrounding code already uses callbacks, call the supplied function with the value when it is ready:
function getValueLater(callback) {
setTimeout(() => {
callback(getValue());
}, 1000);
}
getValueLater((value) => {
console.log(value);
});
Put code that depends on value inside the callback. Statements after getValueLater(...) continue running before the timer callback executes.
Rank #2
Return a Promise for the eventual value
For code that needs to compose asynchronous steps, return a Promise. Resolve it with the value inside the timer callback; callers can then use await or .then().
function getValueLater() {
return new Promise((resolve) => {
setTimeout(() => {
resolve(getValue());
}, 1000);
});
}
async function main() {
const value = await getValueLater();
console.log(value);
}
main();
await works here because getValueLater() returns a Promise. Using await setTimeout(...) directly does not wait for a Promise containing the callback’s result: the timer call itself returns an identifier. If the delayed operation can fail, the Promise should also reject on failure so the caller can handle that error.
References: MDN Web Docs: Promise; MDN Web Docs: Using promises; MDN Web Docs: async function.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Choose the pattern that fits the caller
| Pattern | How the value arrives | Best fit |
|---|---|---|
| Callback | Invoke a supplied function with the eventual value. | Existing callback-oriented APIs or a small event handler. |
| Promise | Resolve with the value; consume it with await or .then(). |
Composable asynchronous functions or code already using Promises. |
Both patterns handle a future result rather than making the timer synchronous. Choose based on how the surrounding code handles asynchronous work.
Quick Recap
Best Value
Rank #4
Common mistakes
- Returning from the timer callback and expecting the outer function to return that value: the callback runs later and its return value is not passed to the scheduling caller.
- Returning
setTimeout(): this gives the caller the timer identifier, not the computed result. - Awaiting
setTimeout()directly: the timer call does not return a Promise for the callback’s result; wrap it in a Promise first. - Reading a variable immediately after scheduling: the callback may not have run yet. Use the value inside the callback or after awaiting the Promise.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




