Promise.all(function(){
for(var numb in req.body){
console.log(numb+":"+req.body[numb]);
checkValue(numb,function(err,result){
if(result){
console.log(result);
send[result]="true";
console.log(send);
}
if(err){console.log(err+"not");}
});
}
}).then(res.json(send));
我想先执行for循环,再把数据传回去。我正在尝试使用 promise.all 但我不确定它是否正确。有人可以帮帮我吗?
最佳答案
如果您正在使用 Promises,请查看 this
您可以通过执行以下操作来解决此问题:
var promises = [];
for(var numb in req.body)
{
promises.push(checkValue(numb));
}
Promise.all(promises)
.then(function(data){ /* do stuff when success */ })
.catch(function(err){ /* error handling */ });
function checkValue(numb){
return new Promise(function(resolve, reject){
// place here your logic
// return resolve([result object]) in case of success
// return reject([error object]) in case of error
});
关于node.js - 在 node.js 中循环 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36808876/