0
0
Javascriptprogramming~10 mins

Why promises are used in Javascript - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new Promise.

Javascript
const myPromise = new [1]((resolve, reject) => {
  resolve('Done');
});
Drag options to blanks, or click blank then click option'
APromise
BFunction
CObject
DArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'promise' instead of 'Promise'.
Using 'Function' instead of 'Promise'.
2fill in blank
medium

Complete the code to handle a resolved Promise.

Javascript
myPromise.[1](result => {
  console.log(result);
});
Drag options to blanks, or click blank then click option'
Acatch
Bthen
Cfinally
Dresolve
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'catch' to handle success.
Using 'resolve' as a method.
3fill in blank
hard

Fix the error in the code to handle Promise rejection.

Javascript
myPromise.then(result => {
  console.log(result);
}).[1](error => {
  console.error(error);
});
Drag options to blanks, or click blank then click option'
Afinally
Bthen
Ccatch
Dresolve
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'then' to catch errors.
Using 'finally' to catch errors.
4fill in blank
hard

Fill both blanks to create a Promise that rejects on error.

Javascript
const errorPromise = new Promise(([1], [2]) => {
  [1]('Success');
  [2]('Error occurred');
});
Drag options to blanks, or click blank then click option'
Aresolve
Breject
Cresult
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping resolve and reject.
Using variable names instead of function names.
5fill in blank
hard

Fill all three blanks to create a Promise and handle both success and failure.

Javascript
const promise = new Promise(([1], [2]) => {
  if(true) {
    [1]('Done');
  } else {
    [2]('Failed');
  }
});
promise.[3](message => console.log(message)).catch(error => console.error(error));
Drag options to blanks, or click blank then click option'
Aresolve
Breject
Cthen
Dfinally
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'finally' instead of 'then'.
Mixing up resolve and reject.