I'm using Promise Node.js library and I'd like to do the following:
- Execute async operation that returns a Promise.
- 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:
- Execute async operation that returns a Promise.
- 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 badges1 Answer
Reset to default 6Promise.all
expects an array:
Promise.all([Promise.resolve(2), Promise.resolve(3)]);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961645a4603423.html
评论列表(0条)