Promise和Async/Await用法整理
一、Promise
1、简介
Promise,简单来说就是一个容器,里面保存着某个未来才会结束的时间(通常是一个异步操作的结果)
Promise的基本语法:
new Promise((resolve, reject)=> {
...
} )
从语法上来说,Promise是一个对象,从它可以获取异步操作的消息
基本语法:
let p = new Promise((resolve, reject) => {
resolve(‘success‘)
})
p.then(result => {
console.log(result); //success
})
Promise对象特点和三个状态:
let p = new Promise((resolve,reject) => { //... resolve(‘success‘); console.log(‘after resolve‘); reject(‘error‘); }); p.then(result => { console.log(result); }); p.catch(result => { console.log(result); })
运行结果:
after resolve
success
五一回 更
相关推荐
89500297 2020-06-07
陈旭阳 2020-06-02
前端开发Kingcean 2020-05-29
Magicsoftware 2020-03-28
Bonrui编程路 2020-06-07
niehanmin 2020-05-19
Magicsoftware 2020-03-28
LorenLiu 2020-03-28
lyg0 2020-02-18
88520191 2020-01-30
前端开发Kingcean 2020-01-23
csuzxm000 2020-01-10
whynotgonow 2020-01-05
89500297 2020-01-03
88520191 2019-12-30
Trustport 2019-12-16
wujiajax 2014-01-13
越前 2014-02-08