Querying DynamoDB with Lambda does nothing

前端 未结 6 828
北恋
北恋 2021-02-07 09:01

I have the following code for a Lambda function:

console.log(\'Loading function\');
var aws = require(\'aws-sdk\');
var ddb = new aws.DynamoDB();

function getUs         


        
6条回答
  •  误落风尘
    2021-02-07 09:28

    My problem was that the function I was trying to call accepted a callback.

    Node.js therefore just continued the execution of the Lambda function and once it reaches the end it tells Lambda to shut down because it's done.

    Here's an example of how you can solve it using Promises:

    'use strict';
    
    exports.handler = async (event, context) => {
    
      // WRAP THE FUNCTION WHICH USES A CALLBACK IN A PROMISE AND RESOLVE IT, WHEN
      // THE CALLBACK FINISHES
    
      return await new Promise( (resolve, reject) => {
    
        // FUNCTION THAT USES A CALLBACK
        functionThatUsesACallback(params, (error, data) => {
          if(error) reject(error)
          if(data) resolve(data)
        });
    
      });
    
    };
    

提交回复
热议问题