最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call one function after another function having a async function in itself - Stack Overflow

programmeradmin6浏览0评论

How to call one function a() after another function b() when b() contains a async function c()?

A() {

}

B() {

    //do sometihng
    c(); //async function 
    //do something

}

I want to call A() if B() including c() is done executing. But I can not modify function B().

How to call one function a() after another function b() when b() contains a async function c()?

A() {

}

B() {

    //do sometihng
    c(); //async function 
    //do something

}

I want to call A() if B() including c() is done executing. But I can not modify function B().

Share Improve this question edited Jul 31, 2017 at 16:38 Avinash asked Jul 31, 2017 at 16:30 AvinashAvinash 411 silver badge5 bronze badges 1
  • what is in c()? – charlietfl Commented Jul 31, 2017 at 16:46
Add a ment  | 

2 Answers 2

Reset to default 3
async function b(){
  await c();
}

function a(){}

(async function(){
  await b();
  a();
})()

make b await c, then you can await b and execute a. another way would be:

function b(){

  return c();
}

b().then(a);

the keyword awaitis what you're looking for.

From https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/await : If a Promise is passed to an await expression, it waits for the Promise's resolution and returns the resolved value.

async function c() {
   await b();
   a();
}
发布评论

评论列表(0)

  1. 暂无评论