Which is better, async or sync, for a sync work flow ( NODE JS ) ? | Sololearn: Learn to code for FREE!

0

Which is better, async or sync, for a sync work flow ( NODE JS ) ?

Suppose the client need to read a text file on the server. We have to ways to do that task :- 1. Use fs.readFile() ( an async function) with callbacks or async await then wait for reading and then send the response. 2. Use fs.readFileSync() then wait for reading and send response. I mean the 2nd one ( sync one ) is simpler Also both 1st and 2nd option are blocking the main thread because both are blocking the response for some time. So which one is better in this case ? Plzzz help me to understand !!!

3/21/2022 2:08:24 PM

Abhay

1 Answer

New Answer

0

The async one isn't blocking the main thread. It appears like that, because you are choosing to wait. But sync one on the other hand will block the thread. With the async process, you can keep doing stuff in the main thread. I don't know the specifics of your code. But, I would personally go with the async solution. Shoving everything down to the main thread makes it feel laggy and unresponsive.