0

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.

WilliamX
  • 357
  • 4
  • 17
  • What is `getBufferAsync`? – Bergi Aug 02 '17 at 18:18
  • 2
    That `Promise.all` returns a Bluebird promise doesn't matter. The only relevant thing is the `then` method that creates the promise you're trying to call `spread` on, and that (although not shown) appears not to be the Bluebird one. Start the outer chain with a Bluebird promise. – Bergi Aug 02 '17 at 18:20
  • 1
    Given you're using ES6, you don't need `spread` anyway. Just use array destructuring: `.then(function([buffer, bufferThumb]) { … })` – Bergi Aug 02 '17 at 18:22
  • I am using Bluebird's Promise.promisifyAll(Jimp); ... (later on) Jimp.read(buffer).then(function(image){ // inside here I am returning the Promise.all() *so my assumption is that the thens are Blubird's – WilliamX Aug 02 '17 at 18:58
  • How would you access the values? something like $0.buffer? – WilliamX Aug 02 '17 at 19:01
  • getBufferAsync is just a function that returns a promise. – WilliamX Aug 02 '17 at 19:01
  • The plain text error is: Jimp.read(...).then(...).spread is not a function , where Jimp.read() is a function that returns a promise created by Bluebird with Promise.promisifyAll(Jimp) – WilliamX Aug 02 '17 at 19:14
  • 1
    No. If you have promisified `Jimp`, then the method which will return a Bluebird promise is `Jimp.readAsync`, which calls `Jimp.read` with a callback. But apparently `Jimp` already supports promises out of the box, so you should use `Bluebird.resolve(Jimp.read(buffer)).then(…)` and not call `promisifyAll`. – Bergi Aug 02 '17 at 19:55
  • This is it, I totally overlooked that Jimp.read was originally returning a non-bluebird promise. After the "casting", this is no longer an issue!, Thankx this makes perfect sense. – WilliamX Aug 02 '17 at 20:38

0 Answers0