Home > Common Problem > What are promise objects?

What are promise objects?

小老鼠
Release: 2023-11-01 10:05:02
Original
2460 people have browsed it

The promise object states are: 1. pending: initial state, neither success nor failure state; 2. fulfilled: means the operation was successfully completed; 3. rejected: means the operation failed. Once a Promise object is completed, it will change from the pending state to the fulfilled or rejected state, and cannot change again. Promise objects are widely used in JavaScript to handle asynchronous operations such as AJAX requests and timed operations.

What are promise objects?

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

"Promise" is an object commonly used in asynchronous programming that represents an operation that may complete or fail in the future. Promise objects are widely used in JavaScript to handle asynchronous operations such as AJAX requests, timed operations, file reading and writing, etc.

A Promise object has the following states:

  1. pending: initial state, neither success nor failure state.
  2. fulfilled: means the operation was completed successfully.
  3. rejected: means the operation failed.

Once a Promise object is completed (successful or failed), it will change from the pending state to the fulfilled or rejected state, and cannot change again.

A Promise object contains the following methods:

  1. then(): Returns a Promise. When the Promise is executed, a result value is returned. The result value is passed through the then() method. If the Promise is rejected, the returned Promise will also be rejected.
  2. catch(): Returns a Promise, and when the Promise is rejected, returns a result value. The result value is passed through the catch() method. If the Promise is fulfilled, the returned Promise will be rejected.
  3. finally(): Returns a Promise. Regardless of whether the Promise is fulfilled or rejected, the callback function specified in finally will be executed.
  4. done(): Returns a Promise. When the Promise is fulfilled or rejected, the specified callback function will be called. The difference from finally() is that if the Promise is fulfilled, the callback function will receive the result value of the Promise as a parameter; if the Promise is rejected, the callback function will not receive any parameters.
  5. promise(): Returns a Promise that wraps an object that may have a Promise interface. This method wraps the original object into a Promise object.
  6. all(): Returns a Promise. When all Promise succeeds, returns an array containing the result value of each Promise; if any Promise fails, the returned Promise will be rejected.
  7. race(): Returns a Promise. When any Promise succeeds or fails, the returned Promise will succeed or fail.

The above is the detailed content of What are promise objects?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template