Home >Web Front-end >JS Tutorial >What are the states of promise objects in es6

What are the states of promise objects in es6

WBOY
WBOYOriginal
2022-03-31 18:38:034481browse

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.

What are the states of promise objects in es6

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What are the states of promise objects in es6

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)})

What are the states of promise objects in es6

[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!

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