undefinedfix
Sign in

How does async / await implement their respective execution

qvevn edited in Wed, 04 May 2022
function getTimeOut1() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('===5000ms===');
        }, 5000);
    });
}

function getTimeOut2() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('===2000ms===');
        }, 2000);
    });
}

How to use async / await to achieve this (separate execution) effect:

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

getTimeOut2().then(res => {
    console.log(res);
});
  • Ordinary await will wait for the return result of a to execute B --- > 8000ms+
const a = await getTimeOut1();
const b = await getTimeOut2();
  • use Promise.all ([... P]), although it is a parallel operation, it will wait for the slowest execution to complete before returning the result. ---> 5000ms+

So I didn't figure out how to use async / await to implement regular parallel callbacks.

5 Replies
kzdci
commented on Wed, 04 May 2022
async function logInOrder(urls) {
  // 并发读取远程URL
  const textPromises = urls.map(async url => {
    const response = await fetch(url);
    return response.text();
  });

  // 按次序输出
  for (const textPromise of textPromises) {
    console.log(await textPromise);
  }
}

In the above code, although the parameter of the map method is an async function, it is executed concurrently, because only the internal async function is executed secondary, and the external is not affected. The later for.. of loop uses await internally, so the sequential output is realized.

I hope this answer can help you, thank you for your opinion!

dinara_pulseway
commented on Wed, 04 May 2022

You can't use async / await to implement the parallel callback you want, because await can only resolve a promise( Promise.all In the end, we will change multiple promises into one, that is, await can only solve one callback, which does not meet the needs of multiple callbacks. If you want to call back multiple times, you can only use. Then multiple times, or encapsulate it into a similar Promise.all And Promise.race Methods (e.g Promise.every )。 If the business is simple, it is recommended to use it twice

getTimeOut1().then(() => {...})
getTimeOut2().then(() => {...})
NWMFRWindsor
commented on Thu, 05 May 2022

Execute first, only wait for the result, not wait for one of the results to execute.

let p1 = getTimeOut1()
let p2 = getTimeOut2()
const a = await p1();
const b = await p2();
Tch
commented on Thu, 05 May 2022

Processing method: encapsulate another async function

const callback = {
    async a() {
        await getTimeOut1();
        do sth...
    },
    async b() {
        await getTimeOut2();
        do sth...
    }
};
async () => {
    await Promise.all([
        callback.a(),
        callback.b()
    ]);
} 
blltt
commented on Thu, 05 May 2022

You two don't depend on each other. Just write two async functions. Of course, a function can only describe a set of synchronization logic, JS has no goto

lock This question has been locked and the reply function has been disabled.