0
0
Javascriptprogramming~20 mins

Why promises are used in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use promises in JavaScript?

Which of the following best explains why promises are used in JavaScript?

ATo handle asynchronous operations and avoid callback hell by chaining actions.
BTo make JavaScript code run faster by using multiple CPU cores.
CTo store data permanently in the browser's memory.
DTo convert synchronous code into asynchronous code automatically.
Attempts:
2 left
💡 Hint

Think about how JavaScript handles tasks that take time, like loading data from the internet.

Predict Output
intermediate
2:00remaining
Output of promise chaining

What will be the output of the following JavaScript code?

Javascript
Promise.resolve(5)
  .then(x => x * 2)
  .then(x => x + 3)
  .then(console.log);
A10
Bundefined
C13
DError
Attempts:
2 left
💡 Hint

Follow the value through each then step.

Predict Output
advanced
2:00remaining
Promise error handling output

What will this code print to the console?

Javascript
Promise.reject('fail')
  .catch(err => 'recovered')
  .then(console.log);
Afail
BError
Cundefined
Drecovered
Attempts:
2 left
💡 Hint

Look at how the catch changes the flow.

🧠 Conceptual
advanced
2:00remaining
Why promises improve code readability

Which statement best describes how promises improve JavaScript code readability?

AThey automatically parallelize all asynchronous tasks for faster execution.
BThey allow asynchronous code to be written in a linear, easy-to-follow style using chaining.
CThey replace all functions with classes for better structure.
DThey force all asynchronous code to run synchronously.
Attempts:
2 left
💡 Hint

Think about how promises let you write steps one after another.

Predict Output
expert
2:00remaining
Promise.all behavior with mixed promises

What will be the output of this code?

Javascript
Promise.all([
  Promise.resolve(1),
  Promise.reject('error'),
  Promise.resolve(3)
])
.then(console.log)
.catch(console.log);
Aerror
B[1, 3]
C[1, 'error', 3]
Dundefined
Attempts:
2 left
💡 Hint

Remember how Promise.all behaves when one promise rejects.