0
0
Javascriptprogramming~20 mins

Why async and await are needed in Javascript - Challenge Your Understanding

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
What is the output of this code using async/await?

Look at this JavaScript code using async and await. What will it print?

Javascript
async function fetchData() {
  return 'data loaded';
}

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

main();
ASyntaxError
BPromise { 'data loaded' }
Cundefined
Ddata loaded
Attempts:
2 left
💡 Hint

Remember that await pauses until the promise resolves and returns the resolved value.

Predict Output
intermediate
2:00remaining
What happens without await in async function?

What will this code print?

Javascript
async function fetchData() {
  return 'hello';
}

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

main();
APromise { 'hello' }
Bhello
Cundefined
DTypeError
Attempts:
2 left
💡 Hint

Without await, the function returns a promise immediately.

🧠 Conceptual
advanced
2:00remaining
Why do we need async and await in JavaScript?

Which of these best explains why async and await are needed in JavaScript?

ATo write asynchronous code that looks and behaves like synchronous code, making it easier to read and maintain.
BTo make all functions run faster by using multiple CPU cores automatically.
CTo replace all callback functions with synchronous blocking calls.
DTo allow JavaScript to run outside the browser.
Attempts:
2 left
💡 Hint

Think about how async/await helps with code readability and flow.

Predict Output
advanced
2:00remaining
What is the output order of this async code?

What will this code print, and in what order?

Javascript
console.log('Start');

async function task() {
  console.log('Inside async');
  return 'Done';
}

task().then(result => console.log(result));

console.log('End');
AStart<br>Done<br>Inside async<br>End
BStart<br>End<br>Inside async<br>Done
CStart<br>Inside async<br>End<br>Done
DStart<br>Inside async<br>Done<br>End
Attempts:
2 left
💡 Hint

Remember that the body of an async function runs immediately until the first await or return.

🧠 Conceptual
expert
2:00remaining
What problem does async/await solve compared to callbacks?

Which statement best describes the main problem that async and await solve compared to traditional callbacks?

AThey automatically parallelize all asynchronous operations without developer control.
BThey prevent callback hell by making asynchronous code easier to write and understand.
CThey make asynchronous code run synchronously, blocking the main thread.
DThey eliminate the need for promises entirely.
Attempts:
2 left
💡 Hint

Think about how callback nesting can get complicated.