0
0
Node.jsframework~20 mins

Async/await syntax in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async/Await Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of async function with await
What will be the output of this Node.js code snippet?
Node.js
async function fetchData() {
  return 'data';
}

async function main() {
  const result = await fetchData();
  console.log(result);
}

main();
AError: await is only valid in async function
BPromise { 'data' }
Cundefined
Ddata
Attempts:
2 left
💡 Hint
Remember that await unwraps the resolved value of a promise inside an async function.
component_behavior
intermediate
2:00remaining
Behavior of async function returning a rejected promise
What happens when this async function is called without a try/catch block?
Node.js
async function fail() {
  throw new Error('fail');
}

fail();
AThe error is silently ignored
BThe program crashes immediately
CA rejected promise is returned but no error is thrown synchronously
DThe error is logged to console automatically
Attempts:
2 left
💡 Hint
Async functions always return promises, even when throwing errors.
📝 Syntax
advanced
2:00remaining
Identify the incorrect usage in async/await
Which option contains incorrect usage of async/await?
Node.js
async function test() {
  const data = await fetchData();
  return data;
}
Aconst data = await fetchData;
Bconst data = await fetchData();
Cawait fetchData();
Dasync function test() { const data = await fetchData(); return data; }
Attempts:
2 left
💡 Hint
Remember that await expects a function call returning a promise, not the function itself.
🔧 Debug
advanced
2:00remaining
Why does this async function never resolve?
Consider this code. Why does the console.log never run?
Node.js
async function waitForever() {
  await new Promise(() => {});
  console.log('Done');
}

waitForever();
AThe promise never resolves or rejects, so await waits forever
BThe console.log is unreachable due to syntax error
CThe function throws an error before console.log
DThe await keyword is used incorrectly
Attempts:
2 left
💡 Hint
What happens if a promise never settles?
🧠 Conceptual
expert
2:00remaining
Effect of missing await in async function call
What is the value of variable result after running this code?
Node.js
async function getValue() {
  return 42;
}

const result = getValue();
A42
BPromise { 42 }
Cundefined
DError: Missing await
Attempts:
2 left
💡 Hint
Async functions always return promises, even if you don't use await.