MonkeyBonkey
MonkeyBonkey

Reputation: 47881

Bluebird promisifyAll doesn't work in a vanilla case

I'm trying to promisify a Pusher.js function and I am a bit confused why the last technique works and the first two don't, since the first two seem to be following the documentation example.

The errors I get are: triggerAsync not defined.

Sample

var Pusher = require('pusher');
var pusher = new Pusher(params);

Promise.promisifyAll(Pusher); //this doesn't work for some reason
var triggerAsync = Promise.promisify(pusher.trigger); //this also doesn't work

Promise.promisifyAll(Object.getPrototypeOf(pusher));  //this works

Upvotes: 2

Views: 342

Answers (1)

sdgluck
sdgluck

Reputation: 27247

...since the first two seem to be following the documentation example.

So is the last! If we take a look at the documentation for Promise#promisifyAll over at bluebirdjs.com (at the bottom of the document) you will find this, which gives a short explanation for why you need to employ this behaviour:

In all of the above cases the library made its classes available in one way or another. If this is not the case, you can still promisify by creating a throwaway instance:

var ParanoidLib = require("...");
var throwAwayInstance = ParanoidLib.createInstance();
Promise.promisifyAll(Object.getPrototypeOf(throwAwayInstance));
// Like before, from this point on, all new instances + even the throwAwayInstance suddenly support promises

So although it doesn't give us a definitive answer for why we need to do this with Pusher, we can at least be sure that it is because it doesn't "[make] its classes available in one way or another". You can also rest assured that you aren't necessarily doing it wrong (you shouldn't use this method unless you have to).

Upvotes: 2

Related Questions