简体   繁体   English

AWS - Lambda函数不等待等待

[英]AWS - Lambda Function not waiting for await

I'm using AWSs API Gateway along with a Lambda function for an API. 我正在使用AWSs API Gateway以及用于API的Lambda函数。

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 在我的Lambda函数中,我有以下(简化)代码但是我发现await sendEmail没有被尊重,相反,它一直返回undefined

exports.handler = async (event) => {
    let resultOfE = await sendEmail("old@old.com", "new@new.com")
    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
        }
    });
}

since you await sendMail , this requires sendMail to return a Promise - your code uses callbacks to handle the asynchrony, so 因为你await sendMail ,这需要sendMail返回一个Promise - 你的代码使用回调来处理异步,所以

  • the async sendMail doesn't do anything (except make sendMail return a Promise that IMMEDIATELY resolves to undefined async sendMail没有做任何事情(除了make sendMail返回一个立即解析为undefined的Promise)
  • you need to change sendMail to return a Promise (and it won't need async since it won't need await 您需要更改sendMail以返回Promise(并且它不需要async因为它不需要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("old@old.com", "new@new.com")
    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) 根据@ThalesMinussi的评论,如果你不提供回调函数, transporter.sendMail返回一个Promise,所以你可以写:( sendEmail现在是异步的)

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;
    }
}

你正在调用另一个函数内部的异步函数,这很好,但是因为它返回了一个你必须等待启动的一个promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM