如何在AWS Lambda函数中使用异步库调用?

时间:2018-10-30 21:00:16

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

我正在尝试在AWS Lambda函数中使用由lob.com创建的nodejs-8.12库。代码如下:

'use strict';


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

  var imagine all the variable defined here

  console.log(front)
  console.log(r)

  // Create the address
  Lob.addresses.create({
    name: r.name,
    email: '',
    phone: '',
    address_line1: r.address1,
    address_line2: r.address2 || "",
    address_city: r.city,
    address_state: r.state,
    address_zip: r.zipcode,
    address_country: r.country
  }, function (err, address) {
    console.log(address)
    if (err) {
      console.log(err);
    } else {
      Lob.postcards.create({
        description: '',
        to: address.id,
        front: front,
        back: back,
      }, function (err, postcard) {
        var card = postcard
        if (err) {
          console.log(err);
        } else {
          console.log('The Lob API responded with this postcard object: ', postcard)

        }
      });
    }
  });


  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

现在,我运行了lamda函数,在运行上面始终执行的代码之前,我可以看到日志记录。但是在lambda关闭之前执行的上述代码中,没有任何控制台日志记录。

我现在意识到该函数没有等待任何回调。我读过某个地方,在调用异步函数时需要使用invoke,但是我不确定使用外部库自行管理请求时如何使用。有没有办法告诉函数等待所有这些完成?

1 个答案:

答案 0 :(得分:0)

我已经解决了您的问题,如下所示:

    'use strict';


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

        //Create the address
        var promise = new Promise((resolve,reject)=> {
            Lob.addresses.create({
                name: r.name,
                email: '',
                phone: '',
                address_line1: r.address1,
                address_line2: r.address2 || "",
                address_city: r.city,
                address_state: r.state,
                address_zip: r.zipcode,
                address_country: r.country
            }, function (err, address) {

                if(err) return reject(err)

                return resolve(address)
            });
        })

        promise.then((data) => {

            Lob.postcards.create({
                description: '',
                to: data.id,
                front: front,
                back: back,
            }, function (err, postcard) {

                var message = 'Go Serverless v1.0! Your function executed successfully!'
                var code = 200

                if(err)
                {
                    message = err;
                    code = 500
                }

                return callback(null, {
                    statusCode: code,
                    body: JSON.stringify({
                        message: message,
                        input: event,
                    }),
                });

            });
        }).catch(reason => {

            return callback(null, {
                statusCode: 500,
                body: JSON.stringify({
                    message: reason,
                    input: event,
                }),
            });
        })
    };

我用promises来解决第一个调用,一旦解决了它,就调用了第二个方法。

我为您准备了一个示例(异步,回调和Promise): Here working

您必须修改您的代码!

希望能帮助您!