October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Get a Function’s Value from setTimeout in JavaScript

setTimeout schedules a callback and returns a timer identifier. Pass a delayed result to a callback or wrap the timer in a Promise to use it later.
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(). 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.

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

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.

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.

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

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.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.