多个等待的javascript异步 [英] javascript async with multiple await

查看:73
本文介绍了多个等待的javascript异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个api请求可以异步工作,但是第二个请求在第一个请求完成之前就可以正常工作.如何使第二个api请求在完成第一个请求之后开始,然后在第二个请求之后执行第三次?

I have three api request to work asynchronous, but the second request is working before the first request is completed. How do I get the second api request to start after completing the first request and then third after the second?

async doSave(event)
{
  await this.server.post('/insertRecord',{name:'joe'});

            //have to work after completion of above request

  await this.server.post('/changeCountry',{countryName:'US'});

          //have to work after completion of above request

  await this.server.post('/satge',{stage:'fifth'});
}

推荐答案

如果第二个请求在第一个请求之前开始,则表示 this.server.post 返回的Promise在请求前已解决.已经完成.或者,它根本不返回Promise,在这种情况下,它会启动您的异步请求,然后在 this.server.post 返回之后在此函数中运行下一行.

If your second request starts before your first request, it means that the Promise returned by this.server.post resolves before the request is complete. Alternatively, it does not return a Promise at all in which case it starts your asynchronous request, and then runs the next line in this function after this.server.post returns.

这篇关于多个等待的javascript异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆