# How does a return in setTimeout() work?

**URL:** <https://forum.codewithmosh.com/t/how-does-a-return-in-settimeout-work/6879>\
**Category:** Node\
**Created:** [August 18, 2021, 5:03pm UTC](https://forum.codewithmosh.com/t/how-does-a-return-in-settimeout-work/6879 "2021-08-18T17:03:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushikdr](https://avatars.discourse-cdn.com/v4/letter/k/4da419/32.png) [@kaushikdr](https://forum.codewithmosh.com/u/kaushikdr)\
**Post date:** [August 18, 2021, 5:03pm UTC](https://forum.codewithmosh.com/t/how-does-a-return-in-settimeout-work/6879/1 "2021-08-18T17:03:53Z")

</div>

I remember in the lecture, Mosh was talking about how the return value of a function with its return in setTimeout would be updated after the value was eventually returned.

I am not seeing that, however, when I write my own code:

```auto
function asyncBoilWater() {
	setTimeout(() => {
		return ("Water is boiled.")
	}, 3000);
}
let isWaterBoiled = "whatever";
isWaterBoiled = asyncBoilWater();
console.log(isWaterBoiled);
setTimeout(() => console.log(isWaterBoiled), 5000);

```

I am getting `undefined` and then another `undefined`. Did I misunderstand him?

---

<div class="post-metadata">

**Author:** ![Ruu](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/ruu/32/9008_2.png) [@Ruu](https://forum.codewithmosh.com/u/Ruu)\
**Post date:** [August 18, 2021, 6:25pm UTC](https://forum.codewithmosh.com/t/how-does-a-return-in-settimeout-work/6879/2 "2021-08-18T18:25:24Z")

</div>

You need to wrap it in a new Promise like

```auto
function asyncBoilWater() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("Water is boiled.")
    }, 3000);
  })
}

let isWaterBoiled = "whatever";
isWaterBoiled = asyncBoilWater();
console.log(isWaterBoiled);
setTimeout(() => console.log(isWaterBoiled), 5000);

```

Then you should get “pending” as your first log and “Water is boiled.” as your second.
