0
0
Javascriptprogramming~20 mins

Async function syntax in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
AHello!
BPromise { 'Hello!' }
CSyntaxError
Dundefined
Attempts:
2 left
💡 Hint
Remember that async functions always return a Promise, but .then unwraps the value.
Predict Output
intermediate
2: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);
A42
BPromise { 42 }
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
The await keyword unwraps the Promise's resolved value.
Predict Output
advanced
2:00remaining
Error when missing async keyword
What error does this code produce?
Javascript
function fetchData() {
  await Promise.resolve('data');
}

fetchData();
AReferenceError
BNo error, runs fine
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint
await can only be used inside async functions.
Predict Output
advanced
2:00remaining
Return value of an async arrow function
What is the output of this code?
Javascript
const asyncArrow = async () => 10;

asyncArrow().then(console.log);
ASyntaxError
BPromise { 10 }
C10
Dundefined
Attempts:
2 left
💡 Hint
Async arrow functions return Promises resolved with the returned value.
🧠 Conceptual
expert
2: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();
A5
BPromise { 5 }
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Async functions always return a Promise, even if no await is used.