Bird
Raised Fist0
Node.jsframework~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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

Practice

(1/5)
1. What is the main purpose of using Promises in Node.js?
easy
A. To store data permanently on disk
B. To make the program run faster by using multiple CPUs
C. To write synchronous code only
D. To handle asynchronous tasks without freezing the program

Solution

  1. Step 1: Understand asynchronous tasks

    Asynchronous tasks take time and can block the program if not handled properly.
  2. Step 2: Role of Promises

    Promises allow handling these tasks without freezing the program by running code after the task finishes.
  3. Final Answer:

    To handle asynchronous tasks without freezing the program -> Option D
  4. Quick Check:

    Promises manage async tasks = A [OK]
Hint: Promises help avoid freezing during slow tasks [OK]
Common Mistakes:
  • Thinking Promises speed up code execution
  • Confusing Promises with synchronous code
  • Believing Promises store data permanently
2. Which of the following is the correct way to create a Promise in Node.js?
easy
A. const p = Promise(() => { resolve(); });
B. const p = new Promise((resolve, reject) => { /* code */ });
C. const p = new Promise(resolve, reject);
D. const p = Promise.new((resolve, reject) => { });

Solution

  1. Step 1: Check Promise constructor syntax

    The Promise constructor requires a function with two parameters: resolve and reject.
  2. Step 2: Validate each option

    const p = new Promise((resolve, reject) => { /* code */ }); correctly uses new Promise with a function taking resolve and reject.
  3. Final Answer:

    const p = new Promise((resolve, reject) => { /* code */ }); -> Option B
  4. Quick Check:

    Correct Promise syntax = B [OK]
Hint: Use 'new Promise' with (resolve, reject) function [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Passing resolve and reject outside a function
  • Using incorrect Promise constructor syntax
3. What will the following code output?
const promise = new Promise((resolve) => {
  setTimeout(() => resolve('Done'), 100);
});
promise.then(result => console.log(result));
console.log('Start');
medium
A. Start only
B. Done\nStart
C. Start\nDone
D. Done only

Solution

  1. Step 1: Understand asynchronous setTimeout

    The setTimeout delays resolve by 100ms, so 'Done' logs after delay.
  2. Step 2: Order of console logs

    'Start' logs immediately, then after 100ms 'Done' logs from the promise.
  3. Final Answer:

    Start\nDone -> Option C
  4. Quick Check:

    Async delay means 'Start' first, then 'Done' [OK]
Hint: Immediate logs appear before delayed Promise results [OK]
Common Mistakes:
  • Assuming Promise resolves immediately
  • Expecting 'Done' before 'Start'
  • Ignoring asynchronous behavior of setTimeout
4. Identify the error in this Promise code:
const p = new Promise((resolve, reject) => {
  resolve('Success');
  reject('Error');
});
p.then(result => console.log(result))
 .catch(error => console.log(error));
medium
A. Calling reject after resolve has no effect
B. Missing catch block for errors
C. Promise constructor missing 'new' keyword
D. resolve and reject parameters are reversed

Solution

  1. Step 1: Understand Promise state changes

    Once a Promise is resolved or rejected, further calls to resolve or reject are ignored.
  2. Step 2: Analyze code behavior

    The code calls resolve first, so reject after that does nothing.
  3. Final Answer:

    Calling reject after resolve has no effect -> Option A
  4. Quick Check:

    Promise settles once; later calls ignored [OK]
Hint: Promise settles once; ignore calls after resolve/reject [OK]
Common Mistakes:
  • Expecting both resolve and reject to run
  • Forgetting Promise settles only once
  • Confusing order of resolve and reject calls
5. You want to run two async tasks one after another using Promises. Which code correctly chains them to run sequentially?
hard
A. task1().then(() => task2()).then(result => console.log(result));
B. Promise.all([task1(), task2()]).then(results => console.log(results));
C. task1(); task2(); console.log('Done');
D. task1().catch(() => task2()).then(result => console.log(result));

Solution

  1. Step 1: Understand sequential chaining

    To run tasks one after another, call the second in the first's .then() callback.
  2. Step 2: Analyze options

    task1().then(() => task2()).then(result => console.log(result)); chains task2 after task1 completes, ensuring order.
  3. Final Answer:

    task1().then(() => task2()).then(result => console.log(result)); -> Option A
  4. Quick Check:

    Chain with .then() for sequential async tasks [OK]
Hint: Use .then() chaining to run tasks one after another [OK]
Common Mistakes:
  • Using Promise.all for sequential tasks (runs parallel)
  • Calling tasks without chaining (runs parallel)
  • Misusing catch to run second task