我必须等待func1才能运行func2。但是由于func1 / 2/3包含 promise ,因此它可以“确定”打印到早期。

async function executeAsyncTask () {
              const res1 = await func1(a,b,c)
              const res2 = await func2(a,b,c)
              const res3 = await func2(a,b,c)

              return console.log(res1 , res2 , res3 )
            }

executeAsyncTask ()

func1
class A{

    promise_API_CALL(params){
      //some code here..
    }

    func1(a,b,c){

    //so work here...

    this.promise_API_CALL(params, function( data, err ) {
       if(err){console.error(err)}
      console.log( data );
      return data;
    });

    //so work here...
    console.log("termined")


}

编辑:promise_API_CALL是外部库的功能

最佳答案

尝试将api调用包装在promise中。否则,我将无法按照您希望的方式看到此效果:

func1(a, b, c) {
  return new Promise((resolve, reject) => {
    this.promise_API_CALL(params, function(data, err) {
      if (err) {
        console.error(err)
        reject(err);
      }

      console.log(data);
      resolve(data);
    });

    //so work here...
    console.log("termined")
  });
}

08-25 11:56
查看更多