'fs.readFile operation inside the callback of parent fs.readFile does not execute asynchronously

I have read that fs.readFile is an asynchronous operation and the reading takes place in a separate thread other than the main thread and hence the main thread execution is not blocked. So I wanted to try out something like below

// reads take almost 12-15ms
fs.readFile('./file.txt', (err, data) => {
  console.log('FIRST READ', Date.now() - start)
  
  const now = Date.now()

  fs.readFile('./file.txt', (err, data) => {
    // logs how much time it took from the beginning
    console.log('NESTED READ CALLBACK', Date.now() - start)
  })


  // blocks untill 20ms more, untill the above read operation is done
  // so that the above read operation is done and another callback is queued in the poll phase
  while (Date.now() - now < 20) {}

  console.log('AFTER BLOCKING', Date.now() - start)
})

I am making another fs.readFile call inside the callback of parent fs.readFile call. What I am expecting is that the log NESTED READ CALLBACK must arrive immediately after AFTER BLOCKING since, the reading must be completed asynchronously in a separate thread

Turns out the log NESTED READ CALLBACK comes 15ms after the AFTER BLOCKING call, indicating that while I was blocking in the while loop, the async read operation somehow never took place. By the way the while loop is there to model some task that takes 20ms to complete

What exactly is happening here? Or am I missing some information here? Btw I am using windows



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source