Status: 1. Pending status, which is initialized and there is no result in the process; 2. Fulfilled success status, the resolved status will trigger the subsequent then callback function; 3. Rejected failure status, The rejected status will trigger the subsequent catch callback function.
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
Three states
1.pending: Not in the process yet Result
2.resolved: Success
3.rejected: Failure
Status changes
1. pending -> resolved
2. pending -> rejected
State performance
The pending state will not trigger then and catch
The resolved state will trigger the subsequent then callback function
The rejected state will trigger the subsequent catch callback function
then Change the status with catch
then under normal circumstances it will return resolved, if an error is reported, it will return rejected
catch Under normal circumstances, it will return resolved, if an error is reported, it will return rejected
Test question
//第一题(结果会打印出来1,3,返回resolved状态) Promise.resolve().then(()=>{ console.log(1) //1 resolved }).catch(()=>{ console.log(2) }).then(()=>{ console.log(3) // 3 resolved }) //第二题(结果会打印出来1,2,3) Promise.resolve().then(()=>{ console.log(1) //1 throw new Error("error1") //rejected }).catch(()=>{ console.log(2) //2 resolved }).then(()=>{ console.log(3) //3 resolved }) //第三题(结果会打印出来1,2) Promise.resolve.then(()=>{ console.log(1) //1 throw new Error("error1") //rejected }).catch(()=>{ console.log(2) //2 resolved }).catch(()=>{ console.log(3)})
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of What are the states of promise objects in es6. For more information, please follow other related articles on the PHP Chinese website!