Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Bluebird.promisify() #5

Closed
cvburgess opened this issue Jul 31, 2016 · 8 comments
Closed

Issue with Bluebird.promisify() #5

cvburgess opened this issue Jul 31, 2016 · 8 comments

Comments

@cvburgess
Copy link

Howdy!

Tried promisifying the sendEmailBatch function and it gave me a peculiar error...

I tried using the callback style and it works, but promises are a must-have in my current project.

Here's the code:

const Promise = require('bluebird');
const postmark = require('postmark');

const client = new postmark.Client(<api_key>);

const messages = []; // array or message objects
const sendEmails = Promise.promisify(client.sendEmailBatch);

return sendEmails(messages);
Unhandled rejection TypeError: this.processRequestWithBody is not a function
at Client.sendEmailBatch (/<project path>/node_modules/postmark/lib/postmark/Client.js:120:14)
@cvburgess
Copy link
Author

cvburgess commented Jul 31, 2016

As a workaround for now, I am using this hack:

return new Promise((resolve, reject) => {

  client.sendEmailBatch(messages, (error, success) => {

    if (error) reject(error);

    resolve(success);

  });

});

@mansona
Copy link

mansona commented Sep 20, 2016

Hey @cvburgess I have implemented something very similar using a corresponding function on q.js

Essentially Q has two functions for wrapping node style functions: Q.denodeify and Q.nbind

Denodeify should be used for static functions and nbind should be used for functions that are part of an object and make use of this. In this case, you have to use Q.nbind with postmark.sendEmail because it is part of the postmark object.

Here is my code:

const postmark = require('postmark');
const Q = require('q');

const client = new postmark.Client(nconf.get('postmark:api_key'));
const sendEmail = Q.nbind(client.sendEmail, client);

Obviously, this doesn't directly apply to you but you will need to find the corresponding function to Q.nbind in Bluebird.

@atheken
Copy link
Contributor

atheken commented Sep 21, 2016

Hey folks, I just want to acknowledge this issue. I'll have a look at the code and see if it's an issue related to the prototype for Client.

@sudo-suhas
Copy link

I was able to work with bluebird by doing the following:

const Promise = require('bluebird'), // eslint-disable-line no-shadow
    postmark = require('postmark');

const client = new postmark.Client(postmarkConfig.apiToken),
    sendEmailWithTemplate = Promise.promisify(client.sendEmailWithTemplate, {context: client});

// Usage
// Returns a promise
sendEmailWithTemplate({
    From: from,
    To: to,
    TemplateId: postmarkConfig.templates[type],
    TemplateModel: locals
});

@stringbeans
Copy link

as an FYI, this is not an issue with postmark.js but an issue with how you're using Promise.promisify. You must bind the correct context to the function you are "promisifying"

@cvburgess
Copy link
Author

I will try this in my code and close this issue if it works - thanks @stringbeans :)

@atheken
Copy link
Contributor

atheken commented Jul 11, 2017

We just released 1.4.0 of the library, which includes Promise support. To get Promises back, simply call the same client methods without a callback function. The client methods will return a Promise when the callback function is emitted.

@atheken atheken closed this as completed Jul 11, 2017
@cvburgess
Copy link
Author

Thank you @atheken I will update tonight :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants