I am using the bluebird library in a node.js app (node 6.10) where I need to chain several promises.
I noticed bluebird has a .spread function that lets you customize the parameters from a ёPromise.all()ё promise, and this works fine if done in place.
var promise1 = Promise.promisify(image.getBuffer, {context: image});
var promise2 = Promise.promisify(imageThumb.getBuffer, {context: imageThumb});
Promise
.all([promise1(Jimp.AUTO), promise2(Jimp.AUTO)])
.spread(function(buffer, bufferThumb){
console.log("SPREAD IS A FUNCTION");
});
*This Works!!!
but this:
...
var promise1 = Promise.promisify(image.getBuffer, {context: image});
var promise2 = Promise.promisify(imageThumb.getBuffer, {context: imageThumb});
return Promise
.all([getBufferAsync(Jimp.AUTO), getBufferAsyncThumb(Jimp.AUTO)]);
})
.spread(function(buffer, bufferThumb){
// this never gets called:
console.log("SPREAD IS NOT A FUNCTION");});
...
*Doesnt work, I event get an error in the form:
TypeError: blahbla.read(...).then(...).spread is not a function
I managed a workaround using:
...
}).then(function(data){
// use data[0], data[1]
...
But what I really want to do is use .spread in the promise chain. Since I am already using bluebird, I assumed that the Promise returned by Promise.all() is a Bluebird promise and that I can use directly the .spread function. Although it looks like returning a promise to the chain stripes the Bluebird sugar and leaves a simple ES6 promise.
Any thoughts on how to make it work are welcome!
For now I will be using the .then(data), data[0], etc approach.
**The problem is not that the promise wont resolve, the problem is that .spread fails as not being a function in the promise chain. By starting with a function that was originally promisified with Bluebird, the assumption is that the rest of the chain will stick to the Bluebird Promise definition.