userMod2
userMod2

Reputation: 8960

AWS - Lambda Function not waiting for await

I'm using AWSs API Gateway along with a Lambda function for an API.

In my Lambda function, I have the following (simplified) code however I'm finding that the await sendEmail isn't being respected, instead, it keeps returning undefined

exports.handler = async (event) => {
    let resultOfE = await sendEmail("[email protected]", "[email protected]")
    console.log(resultOfE)
}

async function sendEmail(oldEmail, newEmail) {
    var nodemailer = require('nodemailer');

    var transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'xxx',
            pass: 'xxx'
        }
    });

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

Upvotes: 8

Views: 4090

Answers (2)

Jaromanda X
Jaromanda X

Reputation: 1

since you await sendMail, this requires sendMail to return a Promise - your code uses callbacks to handle the asynchrony, so

  • the async sendMail doesn't do anything (except make sendMail return a Promise that IMMEDIATELY resolves to undefined
  • you need to change sendMail to return a Promise (and it won't need async since it won't need await

the code below should do it -

var nodemailer = require('nodemailer'); // don't put require inside a function!!

exports.handler = async (event) => {
    const resultOfE = await sendEmail("[email protected]", "[email protected]")
    console.log(resultOfE)
}

//doesn't need async, since there will be no await
function sendEmail(oldEmail, newEmail) {
    return new Promise((resolve, reject) => { // note, reject is redundant since "error" is indicated by a false result, but included for completeness
        const transporter = nodemailer.createTransport({
            service: 'gmail',
            auth: {
                user: 'xxx',
                pass: 'xxx'
            }
        });
        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                console.log(error);
                resolve(false);
            } else {
                console.log('Email sent: ' + info.response);
                resolve(true);
            }
        });
        // without the debugging console.logs, the above can be just
        // transporter.sendMail(mailOptions, error => resolve(!error));
    });
}

as per comment by @ThalesMinussi, transporter.sendMail returns a Promise if you do not provide a callback function, so you could write: (sendEmail is now async)

async function sendEmail(oldEmail, newEmail) {
    const transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'xxx',
            pass: 'xxx'
        }
    });
    try {
        const info = await transporter.sendMail(mailOptions);
        console.log('Email sent: ' + info.response);
        return true;
        }
    } catch (error) {
        console.log(error);
        return false;
    }
}

Upvotes: 11

Halil Irmak
Halil Irmak

Reputation: 1399

You are invoking an async function inside of another function which is fine, however since it is returning a promise you have to await start as well.

Upvotes: 0

Related Questions