2

I am writing a node server that reads/deletes/adds/etc a file from the filesystem. Is there any performance advantage to reading asynchronously? I can't do anything while waiting for the file to be read. Example:

deleteStructure: function(req, res) {
  var structure = req.param('structure');
  fs.unlink(structure, function(err) {
    if (err) return res.serverError(err);
    return res.ok();
  });
}

I am also making requests to another server using http.get. Is there any performance advantage to fetching asynchronously? I can't do anything while waiting for the file to be fetched. Example:

getStructure: function(req, res) {
  var structure = urls[req.param('structure')];
  http.get(structure).then(
    function (response) {
      return res.send(response);
    },
    function (err) {
      res.serverError(err)
    }
  );
}

If there is no performance advantage to reading files asynchronously, I can just use the synchronous methods. However, I am not aware of synchronous methods for http calls, do any built in methods exist?

FYI I am using Sails.js.

Thanks!

1 Answer 1

7

I can't do anything while waiting for the file to be read.
I can't do anything while waiting for the file to be fetched.

Wrong; you can handle an unrelated HTTP request.

Whenever your code is in the middle of a synchronous operation, your server will not respond to other requests at all.

This asynchronous scalability is the biggest attraction for Node.js.

Sign up to request clarification or add additional context in comments.

1 Comment

You answered the question so fast I can't even accept it yet, thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.