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 是如何被多次记录的,但我敢打赌,您的一些过早的错误处理程序会中断您的控制流。