我正在使用蓝鸟。我需要得到这个函数,即get_like_status(email,results [i] .id),它在我发送标题之前执行for循环的末尾。当我控制数组列表时,我得到它未定义。如果你能用bluebird做到这一点,我将非常感激,但我愿意接受其他任何承诺套餐。
var list = new Array();
console.log(results.length);
function get_like_status(email, project_id){
connection.query("SELECT email FROM likes WHERE liked_by='"+email+"' AND project_id='"+project_id+"';", function(err, results, fields){
if(err){console.log(err); return(err)}else{
if(results.length > 0){
console.log("yes");
return "yes";
}else{
console.log("yes");
return "no";
}
}
})
}
for(var i=0; i < results.length; i++){
var empty = new Array();
list.push(empty);
list[i].push(results[i].email);
list[i].push(results[i].project_name);
list[i].push(results[i].project_description);
list[i].push(results[i].header_image);
list[i].push(results[i].id);
list[i].push(get_like_status(email, results[i].id)); // This gets executed after the program has sent response!
}
Promise.all(list).then(function(){
console.log(list);
res.send(list); });
答案 0 :(得分:2)
你必须重构你的代码才能真正等待promises,现在list
数组只是一个具有不同值的常规数组,它不是一个等待解析的promises数组。 / p>
与此类似的东西
function get_like_status(email, project_id) {
return new Promise(function( res, rej ) {
var query = "SELECT email FROM likes WHERE liked_by='" +
email + "' AND project_id='" + project_id + "';"
connection.query(query, function(err, results, fields) {
if (err) {
rej(err);
} else {
res( results.length > 0 ? "yes" : "no" );
}
});
});
}
var promises = [];
var list = results.map(function(item, index) {
var arr = [
item.email,
item.project_name,
item.project_description,
item.header_image,
item.id
]
promises.push(
get_like_status(item.email, item.id).then(function(res) {
arr.push(res);
return arr;
})
);
})
Promise.all( promises ).then(function() {
res.send(list);
}).catch(function( err ) {
res.send('epic fail!')
})
请注意,如果connection.query
返回一个承诺,则无需将其包含在另一个承诺中,它只是不清楚connection.query
来自何处?