Home > Web Front-end > JS Tutorial > How Can Promise Chaining Simplify Nested Promises in Asynchronous Operations?

How Can Promise Chaining Simplify Nested Promises in Asynchronous Operations?

Barbara Streisand
Release: 2024-12-10 15:10:14
Original
290 people have browsed it

How Can Promise Chaining Simplify Nested Promises in Asynchronous Operations?

Unraveling Nested Promises

Introduction

Nested promises can arise when working with asynchronous operations that build upon each other. In the provided code, the viewFile function makes multiple network requests using boxContentRequest and boxViewerRequest. These requests are currently structured within nested promises, making the code difficult to follow.

Chaining Promises

To eliminate the nested promises, we can employ promise chaining. This technique allows us to connect multiple asynchronous operations in a sequential manner, where the output of one promise becomes the input of the next.

Refactored Code

The refactored code below achieves the desired chaining:

exports.viewFile = function (req, res) {
  var fileId = req.params.id;
  boxContentRequest('files/' + fileId + '/content', req.user.box.accessToken)
    .then(function (response) {
      // Return the next promise directly
      return boxViewerRequest('documents', { url: response.request.href }, 'POST');
    })
    .then(function (response) {
      // Return the final promise directly
      return boxViewerRequest('sessions', { document_id: response.body.id }, 'POST');
    })
    .then(function (response) {
      console.log(response);
    });
};
Copy after login

Explanation

In the refactored code, we return the next promise directly from each then callback. This allows us to chain the promises seamlessly, ensuring that the execution flow follows the desired sequence.

Generic Promise Chaining Pattern

The generic pattern for promise chaining is as follows:

// Option 1: Single-line syntax
somePromise
  .then((r1) => nextPromise)
  .then((r2) => anyValue);

// Option 2: Multi-line syntax
somePromise
  .then(function (r1) {
    return nextPromise;
  })
  .then(function (r2) {
    return anyValue;
  });
Copy after login

Both options will resolve the final promise with the value of anyValue. This pattern provides a concise and elegant way to connect asynchronous operations, simplifying the code structure and improving readability.

The above is the detailed content of How Can Promise Chaining Simplify Nested Promises in Asynchronous Operations?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template