最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to return a Promise.all().then to a function - Stack Overflow

programmeradmin3浏览0评论

I am trying to return a Promise.all() to a function. I tried different ways but it is showing errors

here is my code

// 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
    };
  });
}

I am trying to return a Promise.all() to a function. I tried different ways but it is showing errors

here is my code

// 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
    };
  });
}
Share Improve this question edited Nov 5, 2016 at 7:06 thefourtheye 240k53 gold badges465 silver badges500 bronze badges asked Nov 5, 2016 at 6:58 MidhunsaiMidhunsai 4278 silver badges27 bronze badges 4
  • Promise.all() accept argument as array of promise but you are trying to pass array of function . convert your function call into promise and then try. – Veer Commented Nov 5, 2016 at 7:06
  • You should show the actual errors as well. – thefourtheye Commented Nov 5, 2016 at 7:06
  • Doesn't collection.count(...) just return a count, not a promise? – s3raph86 Commented Nov 5, 2016 at 7:22
  • You can't return the future value of a promise. That's what promises are all about. You return a promise, then wait on it with then. – user663031 Commented Nov 5, 2016 at 8:13
Add a ment  | 

1 Answer 1

Reset to default 6
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. So you have to use .then() again in your countByTitle function.

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));
  });
}
发布评论

评论列表(0)

  1. 暂无评论