Synchronous read or write on Nodejs?

2019-08-09 11:04发布

Why and when should we prefer read/writeFileSync to the asynchronous ones on Nodejs in particular for server applications?

2条回答
Animai°情兽
2楼-- · 2019-08-09 11:38

I prefer to use writeFileSync or readFileSync if it does not make any sense for the program to continue in the event of a failure and there is no other asynchronous work to do. Typically this is at the beginning/end of a program, but I have also used them while "checkpointing" a long running process to make it clear that nothing is changing while the checkpoint is being written to disk.

Of course you can implement this with the asynchronous versions too, it is just more verbose and prone to mistakes.

查看更多
forever°为你锁心
3楼-- · 2019-08-09 11:41

For them are blocking functions, you should ever prefer the async versions in production environments.

Anyway, it could make sense to use them during the bootstrap of your application, as an example if you use to load configurations from files and you don't want either to let your fully initialized components to interact with partially initialized ones or design them to be able to work with each other in such a case.

I cannot see any other meaningful use for them.

查看更多
登录 后发表回答