Multiple async functions - lambda functions

I’m having trouble understanding how to call multiple async functions (either one after another or simultaneously works).

I have two functions that use nodemailer to send out an email. First to myself and the other to the user, then return status.

The following code is the closest I’ve gotten and works on dev, but not on prod. I’m guessing it’s similar to this post here, but different enough that I couldn’t seem to use it by simply assigning the functions to a variable. Unfortunately, the post at the bottom’s link that would delve into more detail is not found.

exports.handler = async (event, context) => {

    let body = JSON.parse(event.body).payload;
    const email = body.email;
    const name = body.name;
    const message = body.data.message;

    var transporter = nodemailer.createTransport({
        service: 'gmail',
        port: 587,
        requireTLS: true,
        auth: {
            user: GMAIL,
            pass: EMAIL_PW,
        }
    });

    await mailSelf(transporter, email, name, message);
    await mailReply(transporter, email);    //mailReply is the same as mailSelf but different mailOptions

    return {statusCode: 200, body: "success!"};

}

async function mailSelf(transporter, email, name, message) {
    let mailOptions = {
        from: GMAIL,
        to: HOTMAIL,
        subject: `name`,
        html: `<h3>Message:</h3><p>${message}</p> from <p>${email}</p>`
    };

    transporter.sendMail(mailOptions, function(error, info){
        if (error) {
            console.log('error message', error);
            //resolve(false)
        } else {
            console.log('Email sent: ' + info.response);
            //resolve(true);
        }
    });
}

I’ve tried to set them to promises with returning resolve(true) and using

Promise.all(promises).then(return {statusCode: 200, body: "responseBody"}

and I’ve also tried passing and using callback functions instead, but both methods end up with a 500 response with “lambda response was undefined. check your function code again”. Both methods will usually seem to send the emails out, though.

Any help is much appreciated, thanks!

Does it help to write your mailSelf function like this? (obviously untested):

const mailSelf = async (transporter, email, name, message) => {
  return new Promise((resolve, reject) => {

    ...
    
    transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
        reject(error)
      } else {
        resolve(info)
      }
    });
  });
};

Tried it your method, unfortunately the same 500 error with “lambda response was undefined. check your function code again”. Tried with just one promise and commented out the other function call.

exports.handler = async (event, context) => {
    const email = JSON.parse(event.body).payload.email;
    const name = JSON.parse(event.body).payload.name;
    const message = JSON.parse(event.body).payload.data.message;

    var transporter = ...

    let promises = [];
    let self = mailSelf(transporter, email, name, message);
//    let reply = await mailReply(transporter, email);
    promises.push(await self);
//    promises.push(reply);

    Promise.all(promises).then(values => {
        return {statusCode: 200, body: "responseBody"};
    });
}

const mailSelf = async (transporter, email, name, message) => {
    return new Promise((resolve, reject) => {
       ...

        transporter.sendMail(mailOptions, function(error, info){
            if (error) {
                console.log('error message', error);
                reject(error);
            } else {
                console.log('Email sent: ' + info.response);
                resolve(info);
            }
        });
    });
}

hey steven, this goes thematically a bit beyond “netlify support” so i am going to move this thread to a more appropriate area of the forums :+1: