Challenge - 5 Problems
Await Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of async function with await
What is the output of this code snippet?
Javascript
async function test() { const result = await Promise.resolve('Hello'); console.log(result); } test();
Attempts:
2 left
💡 Hint
Remember that await unwraps the resolved value of a Promise.
✗ Incorrect
The await keyword waits for the Promise to resolve and returns the resolved value, which is 'Hello'. So console.log prints 'Hello'.
❓ Predict Output
intermediate2:00remaining
Await with non-Promise value
What will this code output?
Javascript
async function example() { const val = await 42; console.log(val); } example();
Attempts:
2 left
💡 Hint
Await can be used with non-Promise values too.
✗ Incorrect
Await treats non-Promise values as resolved Promises and returns the value directly. So val is 42.
❓ Predict Output
advanced2:00remaining
Await inside non-async function
What happens when this code runs?
Javascript
function test() { const result = await Promise.resolve('Done'); console.log(result); } test();
Attempts:
2 left
💡 Hint
Await can only be used inside async functions or modules.
✗ Incorrect
Using await inside a regular function causes a SyntaxError because await is only valid inside async functions or top-level modules.
❓ Predict Output
advanced2:00remaining
Order of execution with await
What is the order of console output when this code runs?
Javascript
async function run() { console.log('Start'); await new Promise(resolve => setTimeout(resolve, 0)); console.log('End'); } run(); console.log('After run');
Attempts:
2 left
💡 Hint
Await pauses the async function but does not block the main thread.
✗ Incorrect
The function logs 'Start', then awaits a Promise that resolves after 0ms, so the main thread continues and logs 'After run' before 'End'.
🧠 Conceptual
expert2:00remaining
Await behavior with rejected Promise
What happens when this code runs?
Javascript
async function test() { try { await Promise.reject('Error occurred'); } catch (e) { console.log('Caught:', e); } } test();
Attempts:
2 left
💡 Hint
Await throws if the Promise rejects, so catch block runs.
✗ Incorrect
The rejected Promise causes await to throw, which is caught by the catch block, logging 'Caught: Error occurred'.