新手:如何在Javascript中使用Promises?

Novice: How to use Promises in Javascript?

我正在使用 AWS 异步 lambda 函数,该函数将数据写入 DynamoDB table 以实现我的 Alexa 智能家居技能。我遇到的问题是它不使用异步写入数据库。但是,我将其转换为同步,它将正常工作。我对 Javascript 没有太多经验,使用承诺的经验为零。我想知道我的 code/promise.

出了什么问题

感谢您的帮助或建议!

exports.handler =  async function (event, context) {

    // Dump the request for logging - check the CloudWatch logs
    console.log("index.handler request  -----");
    sendToDynamo("Signal-01","powerState","ON");

}

function sendToDynamo(endpoint,state,value) {
  console.log("send to dynamo start")

  var ddb = new AWS.DynamoDB({
                  apiVersion: '2012-08-10',
                  region: 'us-east-1'})

  var params = {
      TableName: "TestDB",
      Item: {
          'ItemId':{
            S: endpoint  
          } ,
          'State':{
            S: state  
          } ,
          'Value':{
            S: value  
          } 
      }
  };

  var putObjectPromise = ddb.putItem(params).promise();

    putObjectPromise.then(function(data) {
      console.log('*** Success ***');
    })
    .catch(function(err) {
      console.log(err);
    });
}
exports.handler =  async function (event, context) {

    // Dump the request for logging - check the CloudWatch logs
    console.log("index.handler request  -----");
    let data = await sendToDynamo("Signal-01","powerState","ON").catch(console.log);
    console.log('*** Success ***');
}

function sendToDynamo(endpoint,state,value) {
  console.log("send to dynamo start")

  var ddb = new AWS.DynamoDB({
                  apiVersion: '2012-08-10',
                  region: 'us-east-1'})

  var params = {
      TableName: "TestDB",
      Item: {
          'ItemId':{
            S: endpoint  
          } ,
          'State':{
            S: state  
          } ,
          'Value':{
            S: value  
          } 
      }
  };

  return ddb.putItem(params).promise();
}