我可以提前结束 Promise.each 吗?
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
});
我正在使用 bluebird
,我想知道是否可以提前退出 .each
?
我会做那样的事情。 (未测试)
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();
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
?
我会做那样的事情。 (未测试)
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();