[英]how to return a Promise.all().then to a function
I am trying to return a Promise.all()
to a function. 我试图将
Promise.all()
返回到一个函数。 I tried different ways but it is showing errors 我尝试了不同的方法,但显示出错误
// All this iam doing in my server side controller
Promise = require('bluebird');
function countByTitle(accrdnArray) {
console.log(accrdnArray) // array of objects is printing here
var arrayCount = countfunc(accrdnArray);
console.log(JSON.stringify(arrayCount)) // here it is not printing showing error
}
function countfunc(accrdnArray) {
var array = [];
for (var i = 0; i < accrdnArray.lenght; i++) {
var heading = accrdnArray[i].name;
array.push(mongoCount(heading));
}
return Promise.all(array).then(resultantCount => {
console.log(JSON.stringify(resultantCount)); // resultanCout printing here
return resultantCount
})
// Here i want to return the resultantCount to above function.
}
function mongoCount(heading) {
var mongoQuery = {
"ProCategory.title": heading
}
return Collection.count(mongoQuery).then(function(count) {
return {
name: categoryTitle,
count: count
};
});
}
return Promise.all(array).then(resultantCount =>{
console.log(JSON.stringify(resultantCount )); // resultanCout printing here
return resultantCount;
});
This part of the code returns a Promise object. 这部分代码返回Promise对象。 So you have to use
.then()
again in your countByTitle
function. 因此,您必须在
countByTitle
函数中再次使用countByTitle
.then()
。
function countByTitle(accrdnArray) {
console.log(accrdnArray) // array of objects is printing here
var arrayCount = countfunc(accrdnArray);
arrayCount.then(function(count) {
console.log(JSON.stringify(count));
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.