JS笔记es6

ES6(Promise)

2017-09-15  本文已影响0人  余生筑

参考资料

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

promise的作用

promise的三种状态

resolve(data)-->then()

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

上一篇 下一篇

猜你喜欢

热点阅读