JS: A simple understanding of Promises

Understanding on the level:
Promise is a class. There are 3 states: wait state (default), success state, failure state.
Insert picture description here
For example, if you promise to buy a gift for your girlfriend, you must first promise (wait state), buy You will make her happy (that is the result), and you will not buy it (that is the failure), and your choice will get different results.

Simple experience:

let promise = new Promise((resolve,reject)=>{
    reject('参数')
}).then(data=>{//成功
    console.log(data)
},err=>{//失败
    console.log('err',err)
})

Each promise instance has a .then method. resolve(Success), reject(failure).
Resolve and reject are defined by yourself, you need him to succeed or fail, first of all depends on who you call? (Emphasis); once it succeeds, it cannot fail;
Insert picture description here
here you need to interpolate it. If you manually throw an exception, it is doomed to fail:
Insert picture description here

Published 252 original articles · Like 106 · Visits 30,000+

Guess you like

Origin blog.csdn.net/weixin_42554191/article/details/105528012