Skip to content

Latest commit

 

History

History
28 lines (18 loc) · 1.72 KB

66-Chaining Promises.md

File metadata and controls

28 lines (18 loc) · 1.72 KB

Chaining Promises

Chaining Promises in JavaScript is a technique for handling asynchronous operations where multiple Promise objects are linked together in a chain, with the output of one Promise becoming the input of the next. This allows for more efficient and readable code compared to nested callback functions. Each Promise in the chain can be modified or transformed using methods such as .then(), .catch(), and .finally() to handle success, error, and completion cases respectively.

Chaining promises in JavaScript allows you to execute asynchronous operations in a sequence, where the output of one operation feeds as input to the next one.

Here's an example:

fetch('https://jsonplaceholder.typicode.com/users')
  .then(response => response.json())
  .then(data => {
    console.log(data);
    return fetch(`https://jsonplaceholder.typicode.com/posts?userId=${data[0].id}`);
  })
  .then(response => response.json())
  .then(posts => console.log(posts))
  .catch(error => console.error(error));

In this code, we start by performing a GET request to retrieve a list of users from a public API. We then parse the JSON response using the json() method, and extract the first user from the array.

Next, we use the extracted user's ID to construct a new URL, which we pass to another fetch() call to obtain a list of posts made by that user. Again, we parse the JSON response, and finally log the posts to the console.

If any errors occur while executing the promises, we catch them with the catch() method and log them to the console. The chain of promises ensures that each operation is executed in the correct order, without blocking the main thread, and that the output of one operation becomes the input of the next.