ES6(Promise)

作者: 余生筑 | 来源:发表于2017-09-15 10:49 被阅读0次

参考资料

方老师的解释
阮一峰老师关于异步,如何解决异步的看法
如何用Promise使回调地狱可控

promise的作用

  • 确定异步操作的顺序
  • 对异步操作的有关参数进行监控

promise的三种状态

  • pending(进行中)
  • fulfilles(已成功)

resolve(data)-->then()

  • rejected(已失败)

reject(err)-->catch()

用promise改写一个回调函数

    function asyPrint(callback){
            setTimeout(function(){
                console.log('haha')
            },1000)
            callback('haha');
        }
        asyPrint(function(data){
            alert(data)
        })

用Promise来改写

let asyPrint=()=>new Promise((resolve,reject)=>{
                setTimeout(()=>{
                    let data='haha';
                    console.log(data);
                    return resolve(data)
                },1000)
            })
        
        asyPrint().then(function(data){alert(data)});

PromiseValue

fn().then(fa).then(fb),fa返回给fb的参数被称为PromiseValue,如果fa无返回值,则PromiseValue为undefined

Promise

相关文章

网友评论

    本文标题:ES6(Promise)

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