Asynchronous APIs?


#1

Hoping for a bit of help here. I was asked to create an asyncronous API to handle a few calls. It would perform a few database operations to the database and return a very simple response. So far, I have something like this using Restivus

var OrdersV1 = new Restivus({
  version: 'v1',
  prettyJson: true,
  useDefaultAuth: true
});

OrdersV1.addRoute('orders', {
  authRequired: true
}, {
  // other routes aren't too important
  put: function () {
    var self = this;
    // skip some checks that happen here
    var user = Meteor.users.findOne(this.request.headers['x-user-id']);
    var response = Async.runSync(function (done) {
      OrderCards.update(self.bodyParams.orderId, {
        "$set": {
          "status": "completed",
          "pipeline.completedAt": new Date
        }
      }, function (err, status) {
        if (!err) {
          // skip some operations where I manage the inventory
          Machines.update(user.profiles.machine, { $inc: inventory }, done);
        } else {
          done(err, null);
        }
      });
    });

    var responseJson = response.error ? 
      { 'status': 'fail', 'message': response.error } : 
      { 'status': 'success', 'inventory': Machines.findOne(user.profiles.machine).inventory };
    self.response.write(JSON.stringify(responseJson), 'UTF-8', function () {
      self.done();
    });
  }
});

This does seem like it’s running syncronously though… how can I make an API to run asyncronously?