Home  >  Article  >  How to use promise

How to use promise

小老鼠
小老鼠Original
2023-10-12 17:08:291031browse

"Promise" is a programming concept used to handle asynchronous operations. It can be used to represent the final result of an asynchronous operation. Promise objects have three states: pending (in progress), fulfilled (successful) and rejected (failed). The usage of Promise mainly includes constructors, instance methods (then, catch, finally) and state transitions.

How to use promise

In programs, "promise" is a programming concept used to handle asynchronous operations. It can be used to represent the final result of an asynchronous operation. Promise objects have three states: pending (in progress), fulfilled (successful) and rejected (failed).

The usage of Promise mainly includes the following aspects:

  1. Constructor of Promise: You can use the "new Promise" keyword to create a Promise object.
const  promise  =  new  Promise((resolve,  reject)  =>  {   
   //  异步操作   });

In the constructor, the first parameter is an executor function, which is used to handle asynchronous operations. The second parameter is a resolve function that changes the Promise's status from pending to fulfilled. The third parameter is a reject function, used to change the state of the Promise from pending to rejected.

  1. Instance methods of Promise: Promise objects have two commonly used instance methods, namely "Promise.prototype.then" and "Promise.prototype.catch".
  • then: The callback function used to handle the success of Promise.
promise   
   .then((value)  =>  {   
     //  异步操作成功时的回调   
   })   
   .catch((error)  =>  {   
     //  异步操作失败时的回调   
   });
  • catch: The callback function used to handle Promise failure.
promise   
   .then((value)  =>  {   
     //  异步操作成功时的回调   
   })   
   .catch((error)  =>  {   
     //  异步操作失败时的回调   
   });
  1. The finally method of Promise: a callback function used to execute when the Promise succeeds or fails.
promise   
   .then((value)  =>  {   
     //  异步操作成功时的回调   
   })   
   .catch((error)  =>  {   
     //  异步操作失败时的回调   
   })   
   .finally(()  =>  {   
     //  无论成功或失败都会执行的回调   
   });
  1. State transition of Promise: The state of a Promise object can only change from pending to fulfilled or from pending to rejected. Once the state is converted, it cannot be converted again.
promise   
   .then((value)  =>  {   
     //  异步操作成功时的回调   
   })   
   .catch((error)  =>  {   
     //  异步操作失败时的回调   
   });//  以下代码会报错,因为  promise  的状态已经从  pending  变为  fulfilled  或  rejected,不能再转换   promise.then((value)  =>  {   
   //  异步操作成功时的回调   });

In short, Promise is a programming concept used to handle asynchronous operations. It can be used to represent the final result of an asynchronous operation. Promise objects have three states: pending (in progress), fulfilled (successful) and rejected (failed). The usage of Promise mainly includes constructors, instance methods (then, catch, finally) and state transitions.

The above is the detailed content of How to use promise. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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