簡體   English   中英

Promise藍鳥問題

[英]Issue with Promise bluebird

所以我正在使用nodeJS中的bluebird的Promise.mapSeries。

我遇到了一個奇怪的情況,我不明白出了什么問題。

 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); }); }); }); } 

console.log打印輸出:1 2 3 4 5 6 7

它不會轉到console.log('8'),而是經過很長一段時間后再次啟動並打印1 2 3 ...

您應該嘗試將您的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);
  });
}

盡管我們無法掌握原始代碼中出了什么問題,尤其是不了解如何多次記錄1 ,但我敢打賭您的某些過早的錯誤處理程序會導致控制流程中斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM