自己返回 Promise
function ajax(){
return new Promise((resolve, reject)=>{
做事
如果成功就调用 resolve
如果失败就调用 reject
})
}
var promise = ajax()
promise.then(successFn, errorFn)
Promise 深入阅读:http://www.cnblogs.com/hustskyking/p/promise.html
Promise/A+ 规范:https://segmentfault.com/a/1190000002452115
Promise
function buyFruit(){
return new Promise((resolve, reject)=>{
resolve('success')
reject('fail')
})
}
var promise = buyFruit()
promise
async / await
function buyFruit(){
return new Promise((resolve, reject)=>{
resolve('success')
reject('fail')
})
}
var promise = await buyFruit()
async function fn(){
var result = await buyFruit()
return result
}
var r = await fn()
console.log(r)
async、await
网友评论