ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 208 forks source link

Can we use the Promise.promisify method on the sender.send function() ? #250

Closed ghost closed 8 years ago

ghost commented 8 years ago

I tried using Promise.promisify method of bluebird library, to get the return result as a promise. But I was getting an error while doing this -[TypeError: this.sendNoRetry is not a function], This is the code for it -

var sender = new gcm.Sender('API_KEY');
var sendergcm = Promise.promisify(sender.send);
    return sendergcm(message,{registrationTokens: regTokens})
                .then(function(response){
                    console.log(response);
                    return response;
                }).catch(function(err){
                    console.log(err);
                    return err;
                });  
hypesystem commented 8 years ago

I think this should work if you bind the sender object to the send method in the second line:

var sendergcm = Promise.promisify(sender.send.bind(sender));

sender.send refers to the send function without the context of the sender --- you need to explicitly bind this context.

ghost commented 8 years ago

Ok thanks.