美文网首页
Promise & async / await

Promise & async / await

作者: _William_Zhang | 来源:发表于2018-09-25 15:49 被阅读0次

自己返回 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

相关文章

网友评论

      本文标题:Promise & async / await

      本文链接:https://www.haomeiwen.com/subject/ombkoftx.html