HomeInterview QuestionsWhat is wrong with the following JavaScript code t…

What is wrong with the following JavaScript code that uses async/await?

🟡 Medium Debugging Junior level
1Times asked
Apr 2026Last seen
Apr 2026First seen

💡 Model Answer

The code contains a syntax error because the keyword await is used outside of an async function. In JavaScript, await can only appear inside functions declared with the async keyword. The hello function is defined as a regular function, so the interpreter throws a SyntaxError when it encounters await. To fix the code, declare hello as an async function:

js
async function hello() {
  const x = await sayAfter2Seconds("Hello World");
  console.log(x);
}

Alternatively, you could wrap the call in an immediately invoked async function expression (IIFE):

js
(async () => {
  const x = await sayAfter2Seconds("Hello World");
  console.log(x);
})();

Once the await is inside an async context, the promise returned by sayAfter2Seconds will resolve after 2 seconds, and the string "Hello World" will be logged. The overall time complexity is O(1) because the function performs a constant amount of work regardless of input size.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500