简体   繁体   English

Promise蓝鸟问题

[英]Issue with Promise bluebird

So I'm using Promise.mapSeries from bluebird in nodeJS. 所以我正在使用nodeJS中的bluebird的Promise.mapSeries。

I'm confronted to a weird situation, I don't understand what is going wrong. 我遇到了一个奇怪的情况,我不明白出了什么问题。

 remove: function(req, res) { var ru; return Rules.findOne({ ru_id: parseInt(req.query.ru_id) }).populate('producturl').then(function(_ru) { console.log('1'); ru = _ru; }).then(function() { return Promise.mapSeries(ru.producturl, function(prUrl) { console.log('2'); return Products.findOne(prUrl.p_id).populate('producturl').populate('et_id').then(function(pr) { console.log('3'); if (pr.producturl.length > 1) { return EntityService.removeDirectory(ru.producturl[0].url).then(function() { return; }) } else { console.log('4'); var newUrl = root + '/uploads/Segmentation/' + pr.et_id.name + '/notAffected/' + pr.name; newUrl = stringConversion.removeDiacritics(newUrl); var ur_id = pr.producturl[0].ur_id; return ProductURL.update({ ur_id: ur_id }, { url: newUrl }).then(function() { console.log('5'); return EntityService.moveDirectory(prUrl.url, newUrl).then(function() { console.log('6'); return; }, function(err) { return res.negotiate(err); }) }, function(err) { return res.negotiate(err); }); } }, function(err) { return res.negotiate(err); }); }).then(function() { console.log('7'); return Rules.destroy({ ru_id: parseInt(req.query.ru_id) }).then(function() { console.log('8'); res.ok(); }, function(err) { return res.negotiate(err); }); }); }); } 

The console.log print out : 1 2 3 4 5 6 7 console.log打印输出:1 2 3 4 5 6 7

It doesn't go to the console.log('8') and instead after a long time start again and print 1 2 3 ... 它不会转到console.log('8'),而是经过很长一段时间后再次启动并打印1 2 3 ...

You should try flattening your promise callbacks a bit: 您应该尝试将您的promise回调变平一些:

remove: function(req, res) {
  console.log('0');
  return Rules.findOne({
    ru_id: parseInt(req.query.ru_id)
  }).populate('producturl').then(function(ru) {
    console.log('1');
    return Promise.mapSeries(ru.producturl, function(prUrl) {
      console.log('2');
      return Products.findOne(prUrl.p_id).populate('producturl').populate('et_id').then(function(pr) {
        console.log('3');
        if (pr.producturl.length > 1) {
          return EntityService.removeDirectory(ru.producturl[0].url);
        } else {
          console.log('4');
          var newUrl = root + '/uploads/Segmentation/' + pr.et_id.name + '/notAffected/' + pr.name;
          newUrl = stringConversion.removeDiacritics(newUrl);
          var ur_id = pr.producturl[0].ur_id;
          return ProductURL.update({
            ur_id: ur_id
          }, {
            url: newUrl
          }).then(function() {
            console.log('5');
            return EntityService.moveDirectory(prUrl.url, newUrl);
          }).then(function() {
            console.log('6');
            return;
          });
        }
      });
    });
  }).then(function() {
    console.log('7');
    return Rules.destroy({
      ru_id: parseInt(req.query.ru_id)
    });
  }).then(function() {
    console.log('8');
    res.ok();
  }, function(err) {
    console.log('something went wrong');
    res.negotiate(err);
  });
}

While we cannot grasp what went wrong in your original code, and especially not understand how 1 was logged multiple times, I'd wager that some of your premature error handlers threw off your control flow. 尽管我们无法掌握原始代码中出了什么问题,尤其是不了解如何多次记录1 ,但我敢打赌您的某些过早的错误处理程序会导致控制流程中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM