简体   繁体   中英

Can I end a Promise.each early?

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

I'm using bluebird and I want to know if it's possible to abort out of an .each early?

I would do something like that. (not tested)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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