trebuchet
trebuchet

Reputation: 1481

node / express - render page before db callback?

Full disclosure: I'm very new to the totally asynchronous model.

In my application there are a number of instances where information needs to be committed to the db, but the application can continue on without knowing the result. Is it acceptable to render a page before waiting for a db write to complete?

Upvotes: 0

Views: 193

Answers (2)

luin
luin

Reputation: 1985

Yes. For example:

app.get('/', function(req, res, next) {
  res.jsonp({
    message: 'Hello World!'
  });
  var i = 0;
  while (true) {
    i++;
  }
});

When a user visits '/', he will see the result immediately. But if there is only one node instance is running, when the other user visits '/', he won't receive any response as the only instance is under a infinite loop.

If you have a lot of heavy work to do(for example, CPU-bound works), it's much better to use a message queue such as MSMQ and AMQP instead of having all the works done in the node instance.

Upvotes: 1

balupton
balupton

Reputation: 48620

Sure. But how would you notify the user of an error if something did go wrong? Unless you're doing sockets or ajax or something, requests are the standard way.

Upvotes: 0

Related Questions