async和promise总结

**写法的区别**

ES5正常写法

getAjax(url,(res)=>{ })

 

Promise

get(url).then((res)=>{ })

 

async_await

(async ()=>{

    let res = await get(url)

})()

总结:

1.ES5写法和Promise写法,主要区别在写法的不同,可以让回调函数,划分出去,

在.then的函数里去执行使得代码更加的清晰,也可以将两个不同的参数,划分开来写

2.async和promise的区别,主要在于async是promise的语法糖,这种形式的写法在底层编译之后会自动转换成promise写法

 

Promise实现原理

promise需要实现的功能

function fn(resolve,reject){

      setTimeout(()=>{

             if(true){

                   resolve()

             }else{
                   reject()
            }

     })

}

var p1=new ldPromise(fn)

p1.then(function(res){

      console.log(”这是成功做的事情“)
      console.log(res)

 })

p1.catch(function(res){

      console.log(”这是失败做的事情“)
      console.log(res)
 })

 

p1promise发送了异步操作,必然会有一个未来事件,在未来要执行,这个过程由传入的函数对象fn执行

函数fn里必然由成功执行和失败执行的函数

1创建类构造对象

    
            class ldPromise{
                constructor(fn) {
                    //存成功的事件列表
                    this.successList=[]
                    //存失败的事件类表
                    this.failList=[]
                    //一开始的状态初始pending ,成功fullfilled,失败rejected
                    this.stata="pending"
                    //传进去的函数调用(异步操作的函数内容)
                    fn(this.resolveFn.bind(this),this.rejectFn.bind(this))
                }
            }

构造函数的作用:

 声明成功函数放置的数组对象

声明失败函数放置的数组对象

定义初始化对象

调用传入进来执行异步内容的函数(在未来有成功的结果时调用传入的进去的成功函数,在未来失败时调用传入的进去的失败函数)

 

2 传入成功或者失败时需要调用的函数


            class ldPromise{
                constructor(fn) {
                    //存成功的事件列表
                    this.successList=[]
                    //存失败的事件类表
                    this.failList=[]
                    //一开始的状态初始pending ,成功fullfilled,失败rejected
                    this.stata="pending"
                    //传进去的函数调用(异步操作的函数内容)
                    fn(this.resolveFn.bind(this),this.rejectFn.bind(this))
                }
                then(successFn,failFn){
                    if(typeof successFn  =="function"){
                        //把成功的函数放到成功的队列里面,等成功做什么事情再调用它
                        this.successList.push(successFn)
                    }
                    if(typeof failFn  =="function"){
                        //把失败的函数放到失败的队列里面,等失败做什么事情再调用它
                        this.failList.push(failFn)
                    }
                }
                catch(failFn){
                    if(typeof failFn  =="function"){
                        //把失败的函数放到失败的队列里面,等失败做什么事情再调用它
                        this.failList.push(failFn)
                    }
                        
                }
            }

 

作用:将成功和失败的函数传入至成功和失败的数组里

 

3.定义调用成功和失败的函数


            class ldPromise{
                constructor(fn) {
                    //存成功的事件列表
                    this.successList=[]
                    //存失败的事件类表
                    this.failList=[]
                    //一开始的状态初始pending ,成功fullfilled,失败rejected
                    this.stata="pending"
                    //传进去的函数调用(异步操作的函数内容)
                    fn(this.resolveFn.bind(this),this.rejectFn.bind(this))
                }
                then(successFn,failFn){
                    if(typeof successFn  =="function"){
                        //把成功的函数放到成功的队列里面,等成功做什么事情再调用它
                        this.successList.push(successFn)
                    }
                    if(typeof failFn  =="function"){
                        //把失败的函数放到失败的队列里面,等失败做什么事情再调用它
                        this.failList.push(failFn)
                    }
                }
                catch(failFn){
                    if(typeof failFn  =="function"){
                        //把失败的函数放到失败的队列里面,等失败做什么事情再调用它
                        this.failList.push(failFn)
                    }
                        
                }
                
                resolveFn(res){
                    this.state="fullfilled"
                    this.successList.forEach(function(item,i){
                        
                        //将成功的事件循环调用
                        item(res)
                    })
                }
                rejectFn(res){
                    this.state="rejected"
                    //将注册到的失败所有事件进行调用
                    this.failList.forEach(function(res){
                        item(res)
                    })
                    throw Error(res)
                }
            }

作用:成功时调用数组里的所有函数,失败时调用失败数组里所有的函数

 

4.应用

如何将promise与synce和await结合使用

典型的异步读写回调操作

 

        fs.readFile(path,{flag:"r",encoding:"utf-8"},function(err,data){

            if(err){

                // console.log(err)

                //失败执行的内容

                reject(err)

 

            }else{

                // console.log(data)

                // //成功执行的内容

                resolve(data)

            }

            // console.log(456)

 

        })

 

转化成promise对象

 

new Promise(function(resolve,reject){

        fs.readFile(path,{flag:"r",encoding:"utf-8"},function(err,data){

            if(err){

                reject(err)

            }else{

                resolve(data)

            }

        })

    })

 

由于每次使用都不想写这么多代码就会把这样的写法直接进行函数封装

function fsRead(path){

    return new Promise(function(resolve,reject){

        fs.readFile(path,{flag:"r",encoding:"utf-8"},function(err,data){

            if(err){

                reject(err)

            }else{

                resolve(data)

            }

        })

    })

}

使用的时候就可以使用promise写法

p1 = fsRead(path)  //就可以得到promise对象

p1.then (function(data){

    console.log("输出数据:",data)

})

 

async_await写法

(async()=>{

    let p1 = await  fsRead(path)

})()

异步saync函数调用之后也是一个promise对象需要在,then获取或者再用一个异步async包裹起来也可以用await

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_53851127

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值