Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Understanding Promises | Asynchronous APIs and Promises
Node.js Event Loop and Asynchronous Code

bookUnderstanding Promises

Promises are a modern feature in Node.js that help you manage asynchronous operations more effectively. A Promise represents a value that may be available now, in the future, or never. Promises have three possible states:

  • Pending - the initial state, before the operation completes;
  • Fulfilled - the operation completed successfully;
  • Rejected - the operation failed.

You can attach handlers to a Promise to run code when it is fulfilled or rejected, and you can chain multiple asynchronous operations together for clearer, more manageable code.

index.js

index.js

copy

Promises greatly improve the structure of asynchronous code compared to traditional callbacks. With callbacks, you often end up nesting functions inside each other, leading to code that is hard to read and maintain—a situation known as callback hell.

Promises solve this by allowing you to chain operations using then() for success and catch() for errors. This chaining makes it easier to follow the flow of asynchronous tasks and handle errors in one place, resulting in cleaner and more predictable code.

Note
Note

You can chain multiple asynchronous operations with Promises:

fetchData()
  .then((data) => processData(data))
  .then((result) => saveResult(result))
  .catch((err) => console.error(err));

This ensures each step runs only after the previous one finishes, with all errors handled in a single catch block.

question mark

What is the main purpose of a Promise in Node.js

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 7.69

bookUnderstanding Promises

Sveip for å vise menyen

Promises are a modern feature in Node.js that help you manage asynchronous operations more effectively. A Promise represents a value that may be available now, in the future, or never. Promises have three possible states:

  • Pending - the initial state, before the operation completes;
  • Fulfilled - the operation completed successfully;
  • Rejected - the operation failed.

You can attach handlers to a Promise to run code when it is fulfilled or rejected, and you can chain multiple asynchronous operations together for clearer, more manageable code.

index.js

index.js

copy

Promises greatly improve the structure of asynchronous code compared to traditional callbacks. With callbacks, you often end up nesting functions inside each other, leading to code that is hard to read and maintain—a situation known as callback hell.

Promises solve this by allowing you to chain operations using then() for success and catch() for errors. This chaining makes it easier to follow the flow of asynchronous tasks and handle errors in one place, resulting in cleaner and more predictable code.

Note
Note

You can chain multiple asynchronous operations with Promises:

fetchData()
  .then((data) => processData(data))
  .then((result) => saveResult(result))
  .catch((err) => console.error(err));

This ensures each step runs only after the previous one finishes, with all errors handled in a single catch block.

question mark

What is the main purpose of a Promise in Node.js

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt