1

It seems I have an issue with async on promise. I have tested with sails-mysql, sails-mongo, sails-postgres version 0.10-rc-XX and the problem is happen. But when I use sails-disk, there's no problem. Look my comment below

var storeInvoiceDetail = function( detail ) {
  return function( cb ) {
    cb(null, detail);
  };
}

var getPreviousDetail = ['storeInvoiceDetail', function( cb, results ) {
  var invoiceDetail = results.storeInvoiceDetail;

  PreviousDetail
    .findOne({
      invoice: invoiceDetail.invoice,
      product: invoiceDetail.product.id
    })
    .sort('createdAt desc')
    .exec(cb);
}];

var createPreviousDetail = ['storeInvoiceDetail', function( cb, results ) {
  var invoiceDetail = results.storeInvoiceDetail;

  PreviousDetail
    .create({
      invoice: invoiceDetail.invoice,
      product: invoiceDetail.product.id,
      quantity: invoiceDetail.quantity
    })
    .exec(cb);
}];

var getStockDifference = ['storeInvoiceDetail', 'getPreviousDetail', function( cb, results ) {
  var difference = results.storeInvoiceDetail.quantity - results.getPreviousDetail.quantity;

  cb(null, difference);
}];

// see here
var updateProductStock = ['getPreviousDetail', 'getStockDifference', function( cb, results ) {
  Product
    .findOne(results.getPreviousDetail.product)
    .then(function(product) {
      // imagine the value of 'results.getStockDifference' is 5
      product.stock += results.getStockDifference;
      product.save();

      // when I do log, the output is: 5, but this value is not updated to database
      // It seems 'product.save()' at above is not called
      // maybe this code have issues with 'async' & 'promise'
      // anybody know how to correct this?
      console.log(product.stock);
      cb(null, product.stock);
    });
}];

exports.updateProductStock = function (details) {
  var beforeModifyProductStock = {};

  async.each(details, function( detail, callback ) {
    beforeModifyProductStock = {
      storeInvoiceDetail: storeInvoiceDetail(detail),
      getPreviousDetail: getPreviousDetail,
      createPreviousDetail: createPreviousDetail,
      getStockDifference: getStockDifference,
      updateProductStock: updateProductStock
    };

    async.auto(beforeModifyProductStock, function( err, results ) {
      console.log('now latest stock is ' + results.updateProductStock);
      callback();
    });

  }, function (err) {
    // no action
  });
}
Pewh Gosh
  • 1,031
  • 1
  • 11
  • 29
  • Why are you using `async` here? You already have the way more powerful promises in your code... – Benjamin Gruenbaum Jun 22 '14 at 20:44
  • @Benjamin Because I wanna iterate 'details' variable & throw it to 'updateProductStock' function. It's asynchronous so I can't use _.each. Would you mind if you give me example that only uses 'promise'? – Pewh Gosh Jun 23 '14 at 02:29

1 Answers1

1

.save() is an asynchronous method. Rewrite your updateProductStock function as:

var updateProductStock = ['getPreviousDetail', 'getStockDifference', function( cb, results ) {
  Product
    .findOne(results.getPreviousDetail.product)
    .then(function(product) {
      product.stock += results.getStockDifference;
      // Note the callback argument to .save()
      product.save(function(err, product) {
          console.log(product.stock);
          cb(err, product.stock);
      });    
    });
}];

and you should be okay.

sgress454
  • 24,870
  • 4
  • 74
  • 92