Welcome to the Linux Foundation Forum!

Parallel execution in Node.js.

smita21
smita21 Posts: 1
edited January 23 in LFW211 Class Forum

Hi All, I was going through the chapter 8, callbacks. In one of the asynchronous flow example

const { readFile } = require('fs')
const [ bigFile, mediumFile, smallFile ] = Array.from(Array(3)).fill(__filename)

const print = (err, contents) => {
  if (err) {
    console.error(err)
    return
  }
  console.log(contents.toString())
}
readFile(bigFile, print)
readFile(mediumFile, print)
readFile(smallFile, print)

it is mentioned that if these were of different file sizes the order of execution will be
smallFile, mediumFile and BigFile. However i tried running this example by assigning BigFile-> package.json MediumFile->index.js and smallFile->format.js from the my-package example.
The order of execution was bigFile, MediumFile and smallFile. So my question is how it is asynchronous? it seems that it is executing in the sequential manner. Can you explain a bit more on this or maybe share an example how smallFile will be executed first.
Thank you.

Answers

  • k0dard
    k0dard Posts: 93

    Hello,

    I think that the difference between the files you selected is not significant. Try repeating the exercise with for example some 100Mb file for big file, 1Mb file for medium file and 10Kb file for small file, and see what happens

  • @smita21 @k0dard is correct, if the files are small enough and your HD is fast enough, the operational ordering will affect chronological ordering.

Categories

Upcoming Training