Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can’t get a timer callback’s return value from setTimeout(): the call returns a timer ID immediately, while the callback runs later. To use a value produced by delayed code, pass it to another callback or return a Promise that resolves with the value.

Why the callback’s return value is unavailable

setTimeout() schedules a function to run after a delay; it does not pause the current code until that function finishes. MDN describes it as an asynchronous function that returns immediately after scheduling its callback. The value returned by setTimeout() is a timer identifier, not the result of the callback. See MDN’s setTimeout() reference.

const timerId = setTimeout(() => {
  return getValue(); // Returned only from this callback
}, 1000);

console.log(timerId); // Timer identifier, not getValue()'s result

Returning the timer call from another function does not change that:

function getValueLater() {
  return setTimeout(() => getValue(), 1000);
}

getValueLater() returns the timer identifier. The callback’s return happens in a later, separate function call and does not travel back to the caller.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a callback to pass the eventual value

If the surrounding code already uses callbacks, call the supplied function with the result when the timer fires:

function getValueLater(callback) {
  setTimeout(() => {
    callback(getValue());
  }, 1000);
}

getValueLater((value) => {
  console.log(value);
});

Put work that depends on value inside the callback. Code immediately after getValueLater(...) runs before the timer callback.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Return a Promise when callers need to await the value

A Promise represents a value that may become available later, or a failure. Resolve it inside the timer callback, then let the caller use await or .then(). MDN demonstrates this timer-wrapping pattern in its guide to using promises.

function getValueLater() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(getValue());
    }, 1000);
  });
}

async function showValue() {
  const value = await getValueLater();
  console.log(value);
}

showValue();

Here, getValueLater() returns the Promise—not the eventual value synchronously. The await expression inside the async function produces the value after the Promise resolves. await must be used in an async function or a supported module context; MDN documents its use in async functions.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can also consume the Promise with .then():

getValueLater().then((value) => {
  console.log(value);
});

Use the Promise approach when callers need to compose asynchronous work or control what happens after completion. A Promise returned by .then() can be chained, with each handler’s result determining the next Promise’s value; see MDN’s then() reference.

Choose the pattern that fits the surrounding code

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 eventual value; consume it with .then() or await. Composable asynchronous functions and code already using Promises.

Common mistakes

  • Returning from the timer callback: that return belongs to the callback invocation, not the function that scheduled it.
  • Writing return setTimeout(...): this returns the timer identifier.
  • Writing await setTimeout(...): setTimeout() does not return a Promise for the callback result. Wrap the timer in a Promise first.
  • Reading a variable right after scheduling: the callback has not run yet. Move dependent work into the callback or after the Promise resolves.

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.