How to await a Promise when sending an email through Mailgun

This topic was automatically generated from Slack. You can find the original thread here.

Bharat Kilaru : Hi, I’m using Mailgun and their API recommendation for message sending is

mailgun.messages().send(data, function (error, body) {
  console.log(body);
});

What’s the best way to Promisify this for Pipedream? Considering just this, but thinking about good ways to console outputs.

await new Promise((resolve) => mg.messages().send(data, resolve));

Thanks!

Dylan Sather (Pipedream) : it looks like their main JS client has a messages.create method that returns a Promise: https://github.com/mailgun/mailgun-js#messages

Dylan Sather (Pipedream) : so you should be able to run

await mg.messages.create(...)

Dylan Sather (Pipedream) : but the general approach you’re taking to promisify the callback method should work. If you want to add console.log statements after a message was successfully sent, for example, you can use:

await new Promise(r => mg.messages().send(data, function() { console.log("message sent"); resolve() }))

Dylan Sather (Pipedream) : take a look at util.promisify, as well: https://nodejs.org/api/util.html#util_util_promisify_original

Dylan Sather (Pipedream) : ```
const { promisify } = require(“util”)

const promsifiedSend = promisify(mg.messages().send))

await promisifiedSend(…)

Bharat Kilaru : ah great thanks! the js client especially good suggestion here. Big fan when Pipedream actually just suggests this code as well when you link an app.

Bharat Kilaru : noticed that if you link an app as the first step you can see this:

const mailgun = require("mailgun-js")
const domain = params.domain
const mg = mailgun({apiKey: auths.mailgun.api_key, domain})

const data = {
  from: `${params.from_name} <${params.from_email}>`,
  to: params.to_email_list,
  subject: params.subject,
  text: params.text,
  html: params.html,
}

try {
  this.resp = await mg.messages().send(data)
} catch (err) {
  this.err = err
}

Bharat Kilaru : But if you link an app additionally in a single workflow step, you won’t see these recommendations in the same way

Dylan Sather (Pipedream) : Is your process normally to add apps to steps and then author your own Node code? I wonder if there’s a way for us to surface code snippets in an elegant way when you link an app to a step.

Bharat Kilaru : Yeah exactly - I’m adding apps to steps and then triggering them with my own Node code. Interestingly most of the workflows we’re using are long single-step ones. I wonder if there’s a way we should be using multi-step where we’re not, but the main reason is we have a lot of conditional logic in these steps

Dylan Sather (Pipedream) : it’s a totally appropriate way to use Pipedream. I do a lot of the same for more complex workflows. We do plan to add better flow control logic at a workflow level, so that you can run a loop that contains multiple steps, or run a step conditionally. Then you can take full advantage of the pre-built actions and still apply typical flow control primitives.

Bharat Kilaru : Awesome - excited about the direction you and the team are taking this!

Dylan Sather (Pipedream) : how complex does your conditional logic get within your code? Are you just using if / else statements primarily or are you doing something more complex (e.g. switch statements, multiple nested conditions, etc.)? Just trying to assess whether a basic conditional (if condition is true, run these steps) would suffice for you, or whether you’d still want full code-level control

Bharat Kilaru : definitely on the more complex side. i’m adding firebase and typeform as apps in these steps as well and reading through firestore or iterating through typeforms to send emails based on specific inputs. hard to imagine a simple conditional feature would move us away from the single step implementation.

Dylan Sather (Pipedream) : makes sense, thank for the detail Bharat!

Bharat Kilaru : sure thing. some of my favorite experiences in the app have actually been how easy it is to add an app into a step or when there are those helpful snippets to easily show you how to use an app in Pipedream correctly.

Bharat Kilaru : here’s some context on what we’re building as well: https://www.letterloop.co/

Bharat Kilaru : If I can ever be helpful in writing something up or sharing more about pipedream, let me know. Using Pipedream a lot to make an entirely serverless experience with our app

Dylan Sather (Pipedream) : if you ever have the time, it’d actually be hugely helpful to hear how you’re using Pipedream as a part of your stack, and where you think we do well / where we can improve.