我能早点结束Promise吗?

时间:2016-02-27 15:22:25

标签: javascript promise bluebird

db.Question.findAll({
  where: {
    PassageId: dbPassage.id,
    active: true,
    level: {
      $lte: startLevel,
      $gte: endLevel
    }
  },
  order: [db.Sequelize.fn('RANDOM')]
}).each(function(dbQuestion) {
  // End early if some condition
});

我正在使用bluebird,我想知道是否有可能提前退出.each

1 个答案:

答案 0 :(得分:1)

我会做那样的事情。 (未经测试)

var searchPromise = Promise.resolve();

searchPromise = db.Question.findAll({
  where: {
    PassageId: dbPassage.id,
    active: true,
    level: {
      $lte: startLevel,
      $gte: endLevel
    }
  },
  order: [db.Sequelize.fn('RANDOM')]
}).each(function(dbQuestion) {
  if (condition === true) {
    searchPromise.cancel();
  }
}).cancellable();