我想在地图中的每个循环内进行查询,然后完成循环并执行其他操作:
Promise.map(results, function (item, index) {
return Clubs.findAsync({name: name})
.then(function (err, info) {
if (err) {console.info(err); return err};
console.info(info);
return info;
})
.done(function (info) {
return info;
});
}).done(function (data) {
console.info('done');
req.flash('success', 'Results Updated');
res.redirect('/admin/games/'+selectedLeague);
});
在这种情况下,
done
将在控制台info
之前进行控制台。意思是我不能对数据做任何事情。 最佳答案
从bluebird.done:
.done([[FunctionfuledHandler] [,Function rejectedHandler])->
虚空
像.then()一样,但是任何未处理的拒绝都会在此处结束
作为错误抛出。请注意,蓝鸟通常足够聪明
自行找出未处理的拒绝,因此.done很少
需要。如错误管理部分所述,使用.done是
Bluebird提供了更多的编码风格选择,并用于显式
标志着承诺链的结束。
因此,在您的Promise.map
中,它只会得到undefined
或其他东西的数组,而不是Promise
的数组,因此在获取地图后便会解析地图。使用.then
返回Promise
。
Promise.map(results, function (item, index) {
return Clubs.findAsync({name: name})
.then(function (err, info) {
if (err) {console.info(err); return err};
console.info(info);
return info;
})
// vvvv use `.then` here, not `.done`, done returns nothing, not promise.
.then(function (info) {
return info;
});
}).done(function (data) {
console.info('done');
req.flash('success', 'Results Updated');
res.redirect('/admin/games/'+selectedLeague);
});