my node version: v18.6.0
const fs = require('fs/promises');
async function readFileAsync(){
try{
return await fs.readFile('data.csv', {encoding: 'utf8'})
}catch(err){
throw err;
}
}
async function main(){
console.time('promise')
await readFileAsync()
await readFileAsync()
await readFileAsync()
await readFileAsync()
await readFileAsync()
console.timeEnd('promise')
}
async function main2(){
console.time('promise.all')
await Promise.all([readFileAsync(),readFileAsync(),readFileAsync(),readFileAsync(),readFileAsync()])
console.timeEnd('promise.all')
}
// main()
main2()
There is no time difference between the main(188.444ms) and main2(202.826ms) functions.
there is event loop in nodejs, when request readFileAsync
in Promise.all, this should be run parallel.
but run sychronize and run parellel, there is no time difference..
why this happend?
i expect use promise all is more effective. because, parellel