Skip to content

26.Promise的简单理解 #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
webVueBlog opened this issue Mar 11, 2020 · 1 comment
Open

26.Promise的简单理解 #26

webVueBlog opened this issue Mar 11, 2020 · 1 comment
Labels
JavaScript JavaScript

Comments

@webVueBlog
Copy link
Member

No description provided.

@webVueBlog webVueBlog added the JavaScript JavaScript label Mar 11, 2020
@webVueBlog
Copy link
Member Author

层面上的理解:

Promise是一个类,有3个状态分别是:等待态(默认) 成功态 失败态

打个比方比如承诺要给女朋友买一个礼物,一开始先承诺下来(等待态),买了(就是成功),你会让她高兴(就是结果),你不买了(就是失败),你的选择会得到不同的结果。

简单体会:

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

每个promise实例都有一个.then方法。resolve(成功),reject(失败)。
resolve,reject是自己定义的,你需要他成功或者失败,首先要看自己调用的是谁?

(重点);一旦成功了就不能失败。

如果你手动抛出一个异常那就注定会进失败的结果了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript JavaScript
Projects
None yet
Development

No branches or pull requests

1 participant