Asynchronous Vs synchronous in NodeJS

后端 未结 4 552
自闭症患者
自闭症患者 2021-02-08 01:59

I\'m new to NodeJS. I have seen there are separate asynchronous and synchronous functions for the same task (ex: {fs.writeFile,fs.writeFileSync} , {fs.read, f

4条回答
  •  -上瘾入骨i
    2021-02-08 02:31

    In reference to node.js fs.writeFile doc, it asynchronously writes data to a file. This means if you have following code:

    1. some db operation
    2. some non-related db operation
    

    In such case, the 1st operation will not block 2nd operation. 2nd operation execute immediately after 1st(without waiting to finish)

    However, there are some scenarios like:

    1. some db operation 
    2. some related db operations(which you can't put in callbacks) and forcefully want to be after 1st operation.
    

    Then use fs.writeFileSync.

提交回复
热议问题