Challenge - 5 Problems
Async Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of an async function returning a value
What is the output of this code when run in a modern JavaScript environment?
Javascript
async function greet() { return 'Hello!'; } greet().then(console.log);
Attempts:
2 left
💡 Hint
Remember that async functions always return a Promise, but .then unwraps the value.
✗ Incorrect
The async function greet returns a Promise that resolves to 'Hello!'. Using .then(console.log) prints the resolved value 'Hello!'.
❓ Predict Output
intermediate2:00remaining
Output of an async function with await
What will this code output to the console?
Javascript
async function getNumber() { const num = await Promise.resolve(42); return num; } getNumber().then(console.log);
Attempts:
2 left
💡 Hint
The await keyword unwraps the Promise's resolved value.
✗ Incorrect
The await pauses until the Promise resolves to 42, then returns 42. The .then logs 42.
❓ Predict Output
advanced2:00remaining
Error when missing async keyword
What error does this code produce?
Javascript
function fetchData() { await Promise.resolve('data'); } fetchData();
Attempts:
2 left
💡 Hint
await can only be used inside async functions.
✗ Incorrect
Using await outside an async function causes a SyntaxError.
❓ Predict Output
advanced2:00remaining
Return value of an async arrow function
What is the output of this code?
Javascript
const asyncArrow = async () => 10; asyncArrow().then(console.log);
Attempts:
2 left
💡 Hint
Async arrow functions return Promises resolved with the returned value.
✗ Incorrect
The async arrow function returns a Promise resolved with 10. The .then logs 10.
🧠 Conceptual
expert2:00remaining
Behavior of async function without await
What is the value of variable result after running this code?
Javascript
async function noAwait() { return 5; } const result = noAwait();
Attempts:
2 left
💡 Hint
Async functions always return a Promise, even if no await is used.
✗ Incorrect
The function noAwait returns a Promise resolved with 5. So result is a Promise object.