AWS-Lambda函数不等待等待 [英] AWS - Lambda Function not waiting for await

查看:78
本文介绍了AWS-Lambda函数不等待等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWSs API Gateway以及用于API的Lambda函数.

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

在我的Lambda函数中,我有以下(简化的)代码,但是我发现未遵守 await sendEmail ,而是一直返回 undefined

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("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
        }
    });
}

推荐答案

由于您等待sendMail ,因此需要 sendMail 返回 Promise -您的代码使用回调处理异步,因此

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

  • async sendMail 不执行任何操作(使make sendMail 返回Promise,IMMEDIATELY将其解析为 undefined
  • 您需要更改sendMail以返回Promise(并且它不需要 async ,因为它不需要 await
  • 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("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));
    });
}

根据@ThalesMinussi的评论,如果不提供回调函数,则 transporter.sendMail 将返回Promise,因此可以编写:(sendEmail现在是异步的)

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

这篇关于AWS-Lambda函数不等待等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆