我将数据填充到数组中。我在postgress中向DB发出了很多请求,我想将完成的数组响应给客户端。问题是我在通知阵列完成时无法到达,如何知道过滤器何时完成对db的所有选择调用?
result.filter(function(item){
//console.log(item.followed_by)
var query2 = 'SELECT user_id,action,date,element_refered FROM "notifications" WHERE user_id=' + "'"+ item.followed_by +"'"
dbp.sendDBRequest(query2,'blabla',function(results){
notifications.push(results)
console.log(notifications) //here has data
})
})
console.log(notifications) //here it doesnt have data
我试着用承诺认为我做错了:
var p1 = new Promise(
function(resolve, reject) {
result.filter(function(item){
var query2 = 'SELECT user_id,action,date,element_refered FROM "notifications" WHERE user_id=' + "'"+ item.followed_by +"'"
dbp.sendDBRequest(query2,'blabla',function(results){
notifications.push(results)
})
})
resolve(notifications);
}
);
p1.then(
function(val) {
console.log(notifications)
})
.catch(
function(reason) {
console.log('Manejar promesa rechazada ('+reason+') aquí.');
});
编辑代码:
这是带有promises的代码,但我得到的不是过滤器(...)。那么它不是函数
var notifications = []
var query = 'SELECT followed_by FROM "relations" WHERE follower=' +"'"+32+"'"
dbp.sendDBRequest(query,'blabla', function(result) {
result.filter(function(item) {
return new Promise(function(resolve, reject) {
var query2 = 'SELECT user_id,action,date,element_refered FROM "notifications" WHERE user_id=' + "'" + item.followed_by + "'"
dbp.sendDBRequest(query2, 'blabla', function(results) {
notifications.push(results);
resolve(notifications);
})
})
.then(function(val) {
console.log(notifications)
})
.catch(function(reason) {
console.log('Manejar promesa rechazada (' + reason + ') aquí.');
});
})
.then(function(response){
var secondResponse = response + '2';
return response;
})
.then(function(secondResponse) {
//facultad
})
.catch(function(err){
console.log(err)
})
})
答案 0 :(得分:1)
是的,您需要在main函数内部保证,它将返回promise对象。
result.filter(function(item) {
new Promise(function(resolve, reject) {
var query2 = 'SELECT user_id,action,date,element_refered FROM "notifications" WHERE user_id=' + "'" + item.followed_by + "'"
dbp.sendDBRequest(query2, 'blabla', function(results) {
notifications.push(results);
resolve(notifications);
})
})
.then(function(val) {
console.log(notifications)
})
.catch(function(reason) {
console.log('Manejar promesa rechazada (' + reason + ') aquí.');
});
})
然后你可以处理然后捕获块。 此外,如果您想继续,您可以返回该Promise对象并在末尾添加额外的... catch
result.filter(function(item) {
return new Promise(function(resolve, reject) {
var query2 = 'SELECT user_id,action,date,element_refered FROM "notifications" WHERE user_id=' + "'" + item.followed_by + "'"
dbp.sendDBRequest(query2, 'blabla', function(results) {
notifications.push(results);
resolve(notifications);
})
})
.then(function(val) {
console.log(notifications)
})
.catch(function(reason) {
console.log('Manejar promesa rechazada (' + reason + ') aquí.');
});
})
.then(function(response){
var secondResponse = response + '2';
return response;
})
.then(function(secondResponse){
...
})
.catch(function(err){
console.log(err)
})
UPDATE
我认为这会做你想要完成的事情
var notifications = [];
var pMain = new Promise(function(resolve, reject) {
var query = 'SELECT followed_by FROM "relations" WHERE follower=' + "'" + 32 + "'"
dbp.sendDBRequest(query, 'blabla', function(result) {
resolve(result);
})
});
pMain.then(function(p1Result) {
var promises = [];
p1Result.forEach(function(item) {
return promises.push(new Promise(function(resolve, reject) {
var query2 = 'SELECT user_id,action,date,element_refered FROM "notifications" WHERE user_id=' + "'" + item.followed_by + "'"
dbp.sendDBRequest(query2, 'blabla', function(results) {
resolve(results);
});
}));
});
return Promise.all(promises).then(function(results) {
notifications.push(results);
}).then(function(){
console.log(notifications);
});
})