Nodejs - async/await - nested async calls without await is still blocking?

Viewed 105

I would expect the saveData() call in datafx not block anything as there is no "await" but when i check the logs and timestamps i notice that the "main done" log is delayed even though "retrieved data" is not.

async function mainCaller(){
  // do something and then trigger call to get data
  let id = 13;
  let data = await datafx( id );  <-- this gets blocked for saveData() - why?
  LOG( `main done` );
}

async function datafx( id ){
  // get data
  let data = await getData( id );
  // cleanup data
  // save data but don't wait for it
    // datafx does not wait for it as expected
    // however, mainCaller() is somehow "delayed" by it based on log timestamps
  saveData(id, data );
  LOG( `retrieved data` );
  return data;
}

async function getData(id){ return await dbRead(id); }
async function saveData(id, data){ return await dbSave(id, data); }
  1. Is the "await" in the mainCaller() awaiting all async calls, including the nested ones?
  2. Is that correct?
  3. Is there a better way to implement the nested async call to avoid the delay on the caller?
  4. i can confirm that the "saveData()" call is impacting as i removed it and i see the mainCaller continue without delay as soon as datafx returns.
1 Answers

Consider the following code -

function timeoutPromise(interval) {
  return new Promise((resolve, reject) => {
    setTimeout(function () {
      resolve("done");
    }, interval);
  });
};

async function timeTest1() {
  await timeoutPromise(3000);
  await timeoutPromise(3000);
}

async function timeTest3() {
 await timeoutPromise(3000);
}

async function timeTest2() {
  await timeoutPromise(3000);
  timeTest3();
}

async function test1() {
  let startTime = Date.now();
  await timeTest1();
  let finishTime = Date.now();
  let timeTaken = finishTime - startTime;
  console.log("Time taken for test1 in milliseconds: " + timeTaken);
}

async function test2() {
  let startTime = Date.now();
  await timeTest2();
  let finishTime = Date.now();
  let timeTaken = finishTime - startTime;
  console.log("Time taken for test2 in milliseconds: " + timeTaken);
}

async function test3() {
  let startTime = Date.now();
  timeTest1();
  let finishTime = Date.now();
  let timeTaken = finishTime - startTime;
  console.log("Time taken for test3 in milliseconds: " + timeTaken);
}

test1();
test2();
test3();

The output of this program is as follows -

Time taken for test3 in milliseconds: 0
Time taken for test2 in milliseconds: 3004
Time taken for test1 in milliseconds: 6008

Answers to your questions are as follows -

Is the "await" in the mainCaller() awaiting all async calls, including the nested ones?

Consider function test2 in the example code mentioned above, the answer is no.

Is there a better way to implement the nested async call to avoid the delay on the caller?

Yes, you need to remove await from mainCaller while calling datafx function, same as described in function test3

Related