forEach 的简单承诺
Simple promise with forEach
我正在尝试使用 Promise 来等待异步 forEach:
var forEachMatch = function(matches) {
var deferred = Q.defer();
matches.forEach(function(match) {
var messages = Message.find({'matchid': match._id}, function(err,messages) {
if(err)
console.log(err);
else
console.log(match._id);
});
});
return deferred.promise;
};
此处使用:
forEachMatch(matches, function() {
console.log("DONE");
res.status(200).json({
bob,
matches: matches,
});
});
我的控制台输出如下:所有 match._id
被打印但 DONE
从未打印。
有解决办法吗?我从 node 和 promises 开始,所以我当然忘记了一些东西,但我没有看到什么。
感谢您的回答。
编辑
最终解决方案,感谢亚历山大Mac:
var forEachMatch = function(matches) {
var promises = matches.map(match => {
return Message
.find({'matchid': match._id})
.populate({
path: 'fromUser toUser',
select: 'id firstname'
})
.then(messages => [match, messages])
.catch(err => {
console.log("Error in forEachMatch");
return [match, null];
});
});
return Q.all(promises);
}
forEachMatch(matches)
.then(messagesByMatch => {
res.status(200).json({
user,
messagesByMatch: messagesByMatch
});
});
您从未调用过 promise.reject
或 promise.reject
方法。此外,还为异步操作做出了承诺,而 forEach 则不是。
这是一个脏修复:
var forEachMatch = function(matches) {
var deferred = Q.defer();
// make the foreach asynchronous
setTimeout(function() {
matches.forEach(function(match) {
// do stuff
});
// notify end
deferred.resolve();
}, 0);
return deferred.promise;
};
// use .then
forEachMatch(matches).then(function() {
console.log("DONE");
});
在你的情况下匹配更好地使用 Q.all
,它接受承诺或值的数组:
var forEachMatch = function(matches) {
var promises = matches.map(match => {
return Message
.find({'matchid': match._id})
.then(messages => [match, messages]);
});
return Q.all(promises);
}
forEachMatch(matches)
.then(results => {
console.log("DONE", results);
res.status(200).json({
bob,
matches: matches,
});
});
https://github.com/kriskowal/q/wiki/API-Reference#promiseall
您正在返回一个延迟的承诺,并且该承诺没有得到解决或拒绝。尝试以下操作:
var forEachMatch = function(matches) {
var deferred = Q.defer();
matches.forEach(function(match) {
var messages = Message.find({'matchid': match._id}, function(err,messages) {
if(err)
deferred.reject(err);
else
deferred.resolve(match._id);
});
});
return deferred.promise;
};
来电者会变成
forEachMatch(matches).then(id=>{
console.log("done succesffuly found");
}).catch(err=>{
console.log("not found error",err);
}).done(()=>{
console.log("done")
});
我正在尝试使用 Promise 来等待异步 forEach:
var forEachMatch = function(matches) {
var deferred = Q.defer();
matches.forEach(function(match) {
var messages = Message.find({'matchid': match._id}, function(err,messages) {
if(err)
console.log(err);
else
console.log(match._id);
});
});
return deferred.promise;
};
此处使用:
forEachMatch(matches, function() {
console.log("DONE");
res.status(200).json({
bob,
matches: matches,
});
});
我的控制台输出如下:所有 match._id
被打印但 DONE
从未打印。
有解决办法吗?我从 node 和 promises 开始,所以我当然忘记了一些东西,但我没有看到什么。
感谢您的回答。
编辑
最终解决方案,感谢亚历山大Mac:
var forEachMatch = function(matches) {
var promises = matches.map(match => {
return Message
.find({'matchid': match._id})
.populate({
path: 'fromUser toUser',
select: 'id firstname'
})
.then(messages => [match, messages])
.catch(err => {
console.log("Error in forEachMatch");
return [match, null];
});
});
return Q.all(promises);
}
forEachMatch(matches)
.then(messagesByMatch => {
res.status(200).json({
user,
messagesByMatch: messagesByMatch
});
});
您从未调用过 promise.reject
或 promise.reject
方法。此外,还为异步操作做出了承诺,而 forEach 则不是。
这是一个脏修复:
var forEachMatch = function(matches) {
var deferred = Q.defer();
// make the foreach asynchronous
setTimeout(function() {
matches.forEach(function(match) {
// do stuff
});
// notify end
deferred.resolve();
}, 0);
return deferred.promise;
};
// use .then
forEachMatch(matches).then(function() {
console.log("DONE");
});
在你的情况下匹配更好地使用 Q.all
,它接受承诺或值的数组:
var forEachMatch = function(matches) {
var promises = matches.map(match => {
return Message
.find({'matchid': match._id})
.then(messages => [match, messages]);
});
return Q.all(promises);
}
forEachMatch(matches)
.then(results => {
console.log("DONE", results);
res.status(200).json({
bob,
matches: matches,
});
});
https://github.com/kriskowal/q/wiki/API-Reference#promiseall
您正在返回一个延迟的承诺,并且该承诺没有得到解决或拒绝。尝试以下操作:
var forEachMatch = function(matches) {
var deferred = Q.defer();
matches.forEach(function(match) {
var messages = Message.find({'matchid': match._id}, function(err,messages) {
if(err)
deferred.reject(err);
else
deferred.resolve(match._id);
});
});
return deferred.promise;
};
来电者会变成
forEachMatch(matches).then(id=>{
console.log("done succesffuly found");
}).catch(err=>{
console.log("not found error",err);
}).done(()=>{
console.log("done")
});