javascript - Promise.all not working when it's returned from a .then() callback - Stack Overflow

I'm using Promise Node.js library and I'd like to do the following:Execute async operation th

I'm using Promise Node.js library and I'd like to do the following:

  1. Execute async operation that returns a Promise.
  2. When that operation pletes, return a Promise that is pleted when two paralell async operations plete.

The code I'm using is the following:

var Promise = require('promise');

var resolved1 = Promise.resolve(1);

var resolvedAll = resolved1.then(function() {
  return Promise.all(Promise.resolve(2), Promise.resolve(3));
});

var print = resolvedAll.then(function(values) {
  console.log(values);
});

print.done(function () {
  console.log('done');
});

But this code is not working as expected, since it prints:

[]
done

From docu, Promise.all returns a promise that resolves when all of the promises in the iterable argument have resolved. So in theory resolvedAll should not plete until all the promises passed to Promise.all have pleted, but that's not what is happening here.

Any idea why Promise.all not working as expected? Could this be a bug? Anyone aware of alternatives to return multiple promises out of a .then() callback?

Cheers, Galder

I'm using Promise Node.js library and I'd like to do the following:

  1. Execute async operation that returns a Promise.
  2. When that operation pletes, return a Promise that is pleted when two paralell async operations plete.

The code I'm using is the following:

var Promise = require('promise');

var resolved1 = Promise.resolve(1);

var resolvedAll = resolved1.then(function() {
  return Promise.all(Promise.resolve(2), Promise.resolve(3));
});

var print = resolvedAll.then(function(values) {
  console.log(values);
});

print.done(function () {
  console.log('done');
});

But this code is not working as expected, since it prints:

[]
done

From docu, Promise.all returns a promise that resolves when all of the promises in the iterable argument have resolved. So in theory resolvedAll should not plete until all the promises passed to Promise.all have pleted, but that's not what is happening here.

Any idea why Promise.all not working as expected? Could this be a bug? Anyone aware of alternatives to return multiple promises out of a .then() callback?

Cheers, Galder

Share Improve this question asked Mar 21, 2016 at 12:19 Galder ZamarreñoGalder Zamarreño 5,1972 gold badges30 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Promise.all expects an array:

Promise.all([Promise.resolve(2), Promise.resolve(3)]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961645a4603423.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信