javascript - why use sync functions node.js - Stack Overflow

Everyone remends using async (non-blocking) functions instead of sync functions in Node.js .So whats th

Everyone remends using async (non-blocking) functions instead of sync functions in Node.js .

So whats the use of sync functions in node.js if they are not remended?

For example : Why use fs.readFileSync() if fs.readFile() can do the same job without blocking?

Everyone remends using async (non-blocking) functions instead of sync functions in Node.js .

So whats the use of sync functions in node.js if they are not remended?

For example : Why use fs.readFileSync() if fs.readFile() can do the same job without blocking?

Share Improve this question asked Nov 10, 2015 at 18:29 FlakeFlake 1,40618 silver badges32 bronze badges 2
  • stackoverflow./questions/16336367/… It's not that they're not recended, it's just a use case – Sterling Archer Commented Nov 10, 2015 at 18:35
  • Maybe a duplicate of stackoverflow./questions/31863621/… – skypjack Commented Nov 10, 2015 at 20:30
Add a ment  | 

2 Answers 2

Reset to default 11

Sync functions are useful, especially on startup, where you want to make sure that you have the result prior to executing any more code.

For example, you could load in a configuration file synchronously. However, if you are trying to do a file read during a live request, you should use async functions so you don't block other user requests.

Sometimes you want to execute code once you have finished reading or writing to a file. For example

function () {
    array.forEach(function(data) {
        fs.writeFile(data)
    });

    // do this after forEach has finished looping, not after files have finished being writen to
}

As apposed to:

function () {
    array.forEach(function(data) {
        fs.writeFileSync(data)
    });

    // do this after all files have been written to
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742355989a4428444.html

相关推荐

  • javascript - why use sync functions node.js - Stack Overflow

    Everyone remends using async (non-blocking) functions instead of sync functions in Node.js .So whats th

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信