async和await使用

async和await使用

  • Q:假设有3个接口,A,B,C 逐次依赖,求实现方案?

A–>B–>C

  • 模拟代码API
const readFile = function (fileName) {
    return new Promise((resolve, reject) => {
        $.get(fileName).then(_ => {
            resolve(_);
        });
    });
}
  • promise:
    //promise
    readFile('data/a.txt').then(res => {
        console.log(res);
        return readFile('data/b.txt');
    }).then(res => {
        console.log(res);
        return readFile('data/c.txt');
    }).then(res => {
        console.log(res);
    })
  • generator
    function* gen() {
        yield readFile('data/a.txt');
        yield readFile('data/b.txt');
        yield readFile('data/c.txt');
    }
    let g1 = gen();

    g1.next().value.then(res => {
        console.log(res);
        return g1.next().value;
    }).then(res => {
        console.log(res);
        return g1.next().value;
    }).then(res => {
        console.log(res);
    })
    //补充语法
//定义:
		function * gen(){
		    yield 'aaa';
		    yield 'bbb';
		    return 'ccc';
		}
//调用:
		let obj = gen();
		obj.next();  // {value:'aaa', done:false}
		obj.next();  // {value:'bbb', done:false}
		obj.next();  // {value:'ccc', done:true}
  • async
    async function getfile(){
        let f1 = await readFile('data/a.txt');
        let f2 = await readFile('data/b.txt');
        let f3 = await readFile('data/c.txt');
        console.log(f1);
        console.log(f2);
        console.log(f3);
    }
    getfile();

作用

  • 解决深度嵌套
    async function fn(){  //表示异步,这个函数里面有异步任务
        let result = await  xxx	//表示后面结果需要等待
    }  
   function fn(n){
        return new Promise(resolve=>{
            setTimeout(()=>{
                resolve(n);
            },1000);
        });
    }
    async function show(){
        var a=await fn(2);
        var b=await fn(3);
        return a+b;
    }
    show().then(res=>{
        console.log(res);
    });

特点

1. await只能放到async函数中
2. 比genrator语义化更强
3. await后面可以是promise对象,也可以数字、字符串、布尔
4. async函数返回是一个promise对象
5. 只要await语句后面Promise状态变成 reject, 那么整个async函数会中断执行

验证async 返回的是否是promise对象

    async function fn(){
        return 'async';
    }

    fn().then(res=>{
        console.log(res);
    })

验证await语句后面Promise状态变成 reject

    async function fn2() {
      await Promise.reject('this is error');
      let a = await Promise.resolve('this is success');
      console.log(a);
    }

    fn2().then(res => {
      console.log(res);
    }).catch(err => {
      console.log(err);
    })

如何解决async函数中抛出错误,影响后续代码

    try{
		let f1 = await readFile('data/a.txt');
		let f3 = await readFile('data/c.txt');
		let f2 = await readFile('data/b.txt');
	}catch(e){}

业务代码展示

async getInfo(appCode) {
    const oInfo = await approvalAPI.getProppserInfo({
        appCode
    });
    const oMsg = await thirdAPI.loanerGZT({
        appCode,
        type: 2,
        params: `${oInfo.data.proppserName},${oInfo.data.idno}`
    });
    this.checkData.proppserName = oMsg.data.compResult;
},
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值