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

javascript - Node.js Promise.all() hanging - Stack Overflow

programmeradmin1浏览0评论

Kind of stuck on this one. Working with on node.js

exports.count = function ( models, callback ) {
  var promises = [];

  models.forEach(function ( name ) {
    promises.push( countOne( name ) );
  });

  Promise.all( promises ).then(function ( res ) {
    callback( null, res );
  });

  Promise.resolve(promises[0]).then(function (res) {
    console.log('resolved individual with res', res);
  });
};

function countOne ( exampleArg ) {
  return new Promise(function ( resolve, reject ) {
    // It resolves or rejects
  });
}

I've also tried:

Promise.all( promises.map(function ( it ) { return Promise.resolve(it); }) ).then(function ( res ) {
  callback( null, res );
});

Either way, Promise.all doesn't fire the then. Promise.resolve does, however, give the appropriate response.

Kind of stuck on this one. Working with https://github./then/promise on node.js

exports.count = function ( models, callback ) {
  var promises = [];

  models.forEach(function ( name ) {
    promises.push( countOne( name ) );
  });

  Promise.all( promises ).then(function ( res ) {
    callback( null, res );
  });

  Promise.resolve(promises[0]).then(function (res) {
    console.log('resolved individual with res', res);
  });
};

function countOne ( exampleArg ) {
  return new Promise(function ( resolve, reject ) {
    // It resolves or rejects
  });
}

I've also tried:

Promise.all( promises.map(function ( it ) { return Promise.resolve(it); }) ).then(function ( res ) {
  callback( null, res );
});

Either way, Promise.all doesn't fire the then. Promise.resolve does, however, give the appropriate response.

Share Improve this question edited Aug 26, 2014 at 19:15 James_1x0 asked Aug 26, 2014 at 18:24 James_1x0James_1x0 9311 gold badge10 silver badges20 bronze badges 11
  • 1 One (or more) of your promises in the .all isn't resolving and none are rejecting (and it's not promises[0]. So that's that. – Benjamin Gruenbaum Commented Aug 26, 2014 at 18:27
  • 1 this would be a lot cleaner if you just return the promise instead of using a callback, you also arn't propagating error's now – Willem D'Haeseleer Commented Aug 26, 2014 at 18:29
  • The 1st callback to .then() is only for fulfillment (all resolved). You haven't provided anything to be done upon rejection, so it'll fail silently. – Jonathan Lonowski Commented Aug 26, 2014 at 18:32
  • Oh yeah, one of them might actually be rejecting, I forgot then promises don't even detect unhandled exceptions like Bluebird promises or native promises in Firefox do. – Benjamin Gruenbaum Commented Aug 26, 2014 at 18:33
  • 1 @BenjaminGruenbaum I stand corrected – Willem D'Haeseleer Commented Aug 26, 2014 at 18:51
 |  Show 6 more ments

1 Answer 1

Reset to default 3

ether one of your promises isn't resolving, in which case, your need to find out which one.

I think you are trying to do that with that console.log. if you do that with a forEach you can attach a log message to all promises and see which one isn't resolving / rejecting.

Or one of the promises is rejecting, which you are not handling.

I took the liberty of rewriting your code, try it like this:

exports.count = function (models, callback) {
    var allCounts = models.map(countOne);

    // second parameter is the onRejected handler
    Promise.all(allCounts).then(function (res) {
        callback(null, res);
    }, function (err) {
        callback(err);
    });

    promises[0].then(function (res) {
        console.log('resolved individual with res', res);
    });
};

I left in the callback, but I really would just return the promise, much simpler, much cleaner.

ProTip™:
Switch to the bluebird promise library, and replace your implementation with one line:

exports.count = function (models, callback) {
    Promise.map(models, countOne).nodeify(callback);
}

It will probably be faster to.

发布评论

评论列表(0)

  1. 暂无评论