NodeJS和Lamba的Async Await问题

时间:2019-03-13 13:11:35

标签: node.js amazon-web-services async-await aws-lambda

我目前正在为我的AWS项目开发一个Lamba函数调用,但是由于我不是异步函数的大师,看来它正在瓦解,我整理的代码是:

const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");

AWS.config.update({
  region: "us-east-1"
});

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

    //set db
    var documentClient = new AWS.DynamoDB.DocumentClient()

    //params
    const params = {
        Item: {
            'id': uuid.v1(),
            'player_1_name': null,
            'player_1_network': null,
            'player_1_matches': 0,
            'player_1_kills': 0,
            'player_1_last_updated': 0,
            'player_2_name': null,
            'player_2_network': null,
            'player_2_matches': 0,
            'player_2_kills': 0,
            'player_2_last_updated': 0,
            'match_id': 0,
            'status': 0
        },
        TableName : 'matches'
    };

    var matchData = JSON.parse(event.body);

    //player 1
    const player_1_name = matchData.player_1_name ? matchData.player_1_name : null;
    const player_1_network = matchData.player_1_network ? matchData.player_1_network : null;

    //player 2
    const player_2_name = matchData.player_2_name ? matchData.player_2_name : null;
    const player_2_network = matchData.player_2_network ? matchData.player_2_network : null;

    //match data
    const match_id = matchData.match_id ? matchData.match_id : 0;

    //game object
    let gameAPI = new game(
        [
            "email@email.com",
            "password"
        ]
    );

    //gameAPI.login() returns a Promise()
    await gameAPI.login().then(() => {

        //check stats for player 1, getStats returns a Promise()
        gameAPI.getStats(player_1_name, player_1_network).then(stats => {

            params.Item.player_1_matches = stats.lifetimeStats.matches;
            params.Item.player_1_kills = stats.lifetimeStats.kills;

        }).catch(err => {

            //error! we must work out what to do here!
            console.log(err);

        });

        //example insert
        documentClient.put(params, function(err, data){
            return callback(err, data);
        });

    }).catch(err => {
        console.log("We failed to login!");
        console.log(err);
    });

};

此逻辑似乎有缺陷,因为什么都没有扔到我的AWS日志中?我的想法是将请求发送到函数并使其尽快完成,以便我可以将200响应发送回Lambda,有人可以指出正确的方向吗?

1 个答案:

答案 0 :(得分:0)

使用async/await时无需使用callback,也无需陷入无极地狱。

只需await兑现您的承诺就可以取得结果。这里的最大优点是看起来您的代码似乎是同步的。

这是您的重构代码:

const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");

AWS.config.update({
  region: "us-east-1"
});

exports.handler = async (event) => {

    //set db
    var documentClient = new AWS.DynamoDB.DocumentClient()

    //params
    const params = {
        Item: {
            'id': uuid.v1(),
            'player_1_name': null,
            'player_1_network': null,
            'player_1_matches': 0,
            'player_1_kills': 0,
            'player_1_last_updated': 0,
            'player_2_name': null,
            'player_2_network': null,
            'player_2_matches': 0,
            'player_2_kills': 0,
            'player_2_last_updated': 0,
            'match_id': 0,
            'status': 0
        },
        TableName : 'matches'
    };

    var matchData = JSON.parse(event.body);

    //player 1
    const player_1_name = matchData.player_1_name ? matchData.player_1_name : null;
    const player_1_network = matchData.player_1_network ? matchData.player_1_network : null;

    //player 2
    const player_2_name = matchData.player_2_name ? matchData.player_2_name : null;
    const player_2_network = matchData.player_2_network ? matchData.player_2_network : null;

    //match data
    const match_id = matchData.match_id ? matchData.match_id : 0;

    //game object
    let gameAPI = new game(
        [
            "email@email.com",
            "password"
        ]
    );

    //gameAPI.login() returns a Promise()
    await gameAPI.login()

    const stats = await gameAPI.getStats(player_1_name, player_1_network)
    params.Item.player_1_matches = stats.lifetimeStats.matches;
    params.Item.player_1_kills = stats.lifetimeStats.kills;

        //example insert
    await documentClient.put(params).promise();

};

如果需要处理异常(应该),只需将等待调用包装在try/catch块中,如下所示:

try {
   console.log(await somePromise)
} catch (e) {
   console.log(e)
}

上面的代码段等同于:

somePromise.then(console.log).catch(console.log)

与之相去甚远,您无需链接promise /异步代码即可保持执行顺序,因此,我强烈建议您选择async/await方法,而不必考虑.then().catch()