0
0
Node.jsframework~10 mins

Promises for cleaner async in Node.js - Interactive Code Practice

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 that resolves with 'Done'.

Node.js
const myPromise = new Promise((resolve, reject) => {
  resolve([1]);
});
Drag options to blanks, or click blank then click option'
ADone
Breject
Cresolve
D'Done'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using reject instead of resolve
2fill in blank
medium

Complete the code to handle the Promise result with then().

Node.js
myPromise.[1](result => {
  console.log(result);
});
Drag options to blanks, or click blank then click option'
Athen
Bcatch
Cfinally
Dresolve
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch instead of then for success
Trying to use resolve as a method
3fill in blank
hard

Fix the error in the Promise chain to catch errors.

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

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

Node.js
const errorPromise = new Promise((resolve, reject) => {
  if (somethingWrong) {
    reject([1]);
  } else {
    resolve('Success');
  }
});

errorPromise.[2](error => {
  console.log(error);
});
Drag options to blanks, or click blank then click option'
A'Error occurred'
Bcatch
Cthen
D'Success'
Attempts:
3 left
💡 Hint
Common Mistakes
Using resolve instead of reject
Using then instead of catch for errors
5fill in blank
hard

Fill all three blanks to create a Promise, handle success and error, and finally log done.

Node.js
const promise = new Promise((resolve, reject) => {
  if (isReady) {
    resolve([1]);
  } else {
    reject([2]);
  }
});

promise
  .then(result => console.log(result))
  .catch(error => console.error(error))
  .[3](() => console.log('Done'));
Drag options to blanks, or click blank then click option'
A'All good'
B'Failed'
Cfinally
Dcatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch instead of finally for the last step
Forgetting quotes around messages