Part 4: A Basic Async Question That Left Almost Every Interviewee Confused (Don’t Make the Same Mistake)

If you think you’ve “mastered” async JavaScript, read this twice.

A basic Async JavaScript Question

Here Comes The Actual Test

The interviewer drops a file in front of the interviewee. No build tools. No magic. Just plain JS.

And then they ask:

“Why is this code not logging anything?”

async function getNumber() {
return 42;
}
(async () => {
const result = await getNumber();
})();
console.log(result);

👨‍💻 Interviewee— paused for a sec.

Looks Fine, Right?

Let’s break it.

  • getNumber() Returns a Promise.
  • The IIFE (Immediately Invoked Function Expression) awaits it.

So what’s the problem?

The issue is subtle, but deadly in production.

The Gotcha

result is declared inside the async IIFE.
❌ But you’re trying to console.log(result) outside of it.

It’s out of scope. The variable doesn’t exist where you’re trying to log it.

Correct Way?

Here are two:

Option 1: Move the log inside

(async () => {
const result = await getNumber();
console.log(result); // ✅ 42
})();

Option 2: Use .then()

getNumber().then(console.log); // ✅ 42

This Isn’t Just About Interviews

This is how real bugs sneak into your dashboards, APIs, and loading states.

Someone forgets where await is scoped…

And suddenly, your production page says “undefined” while your backend screams in silence, and you’re unaware of what is happening.
I know this looks small but most of the devs I have seen are still confused with the scoping yet.

Test Yourself

Here’s your async interview prep for today 👇

What does this log and why?

const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('done');
}, 0);
});
};
(async () => {
const data = await fetchData();
})();
console.log(data);

Go ahead, explain it without running it.

Comment your answer below. Let’s see who can solve it.

3 Key Reminders

  • await only works inside async functions.
  • Don’t expect scope magic — your const lives where it was declared.
  • Async code needs boundaries. Understand them or get bitten.

Let’s Talk

Want me to do a Part 5 on event loop tricks, Promise.allSettled, or async generators?

Vote in the comments.

At Dev Simplified, We Value Your Feedback 📊

👉 Follow us to not miss any updates.

👉 Have any suggestions? Let us know in the comments!

👉 Subscribe for free and join our growing community!