question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Promisifying mandrill-api

See original GitHub issue

Just getting started with Bluebird and trying to figure out the best way to promisify use of the sendTemplate call on the mandrill-api. It’s not a standard node.js callback design in that it takes 2 callback as the last parameters (first as a success callback and the second being a failure callback). For example (taken from the mandril docs)

mandrill_client.messages.sendTemplate({"template_name": template_name, "template_content": template_content, "message": message, "async": async, "ip_pool": ip_pool, "send_at": send_at}, function(result) {
   // SUCCESS 
    console.log(result);
    /*
    [{
            "email": "recipient.email@example.com",
            "status": "sent",
            "reject_reason": "hard-bounce",
            "_id": "abc123abc123abc123abc123abc123"
        }]
    */
}, function(e) {
    // FAILURE
    // Mandrill returns the error as an object with name and message keys
    console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message);
    // A mandrill error occurred: Unknown_Subaccount - No subaccount exists with the id 'customer-123'
});

Any help/suggestions would be great. At a minimum we’d like to call this from inside a method wrapped in a Promise.method

@pazrul to follow

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
wavdedcommented, Aug 31, 2016

The promise constructor works pretty well with dual callback apis.

// impl
function sendTemplate(opts) {
  return new Promise(function (resolve, reject) {
    mandrill_client.messages.sendTemplate(opts, resolve, reject)
  })
}

// usage
sendTemplate({
  "template_name": template_name,
  "template_content": template_content,
  "message": message,
  "async": async,
  "ip_pool": ip_pool,
  "send_at": send_at
})
.then(handleSuccess)
.catch(handleError)
4reactions
petkaantonovcommented, Oct 8, 2014

If it’s just one method then you can do what @wavded said.

If you want to promisify whole module that has this convention, then use a custom promisifier using the promisifier option:

Promise.promisifyAll(..., {
    promisifier: function(cbApi) {
        return function() {
            var args = [].slice.call(arguments);
            var self = this;
            return new Promise(function(resolve, reject) {
                args.push(resolve, reject);
                cbApi.apply(self, args);
            });
        };
    }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Messages | Mailchimp Transactional API Reference
Returns information about an export job. If the export job's state is 'complete', the returned data will include a URL you can use...
Read more >
Promisifying API callbacks - How to properly resolve or reject
Something like that: function getCurrentPositon(options){ return new Promise(function(resolve, reject){ navigator.geolocation.
Read more >
Using the Mandrill API in my own website - Codecademy
I now have the mailchimp-mandrill-api.js library needed to start working with Mandrill offline. Popular free courses. Course ...
Read more >
Node.js util.promisify() Method - GeeksforGeeks
promisify () method defines in utilities module of Node.js standard library. It is basically used to convert a method that returns responses ...
Read more >
Open Source Used In slido-test 22.06 - Cisco
questions or wish to receive a copy of any source code to which you may be entitled under ... 1.607 mandrill-api 1.0.45 ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found