musecz
musecz

Reputation: 805

Issue with Promise bluebird

So I'm using Promise.mapSeries from bluebird in nodeJS.

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

It doesn't go to the console.log('8') and instead after a long time start again and print 1 2 3 ...

Upvotes: 0

Views: 88

Answers (1)

Bergi
Bergi

Reputation: 664346

You should try flattening your promise callbacks a bit:

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.

Upvotes: 1

Related Questions