How to use Async and Await with AWS SDK Javascript

前端 未结 2 1328
后悔当初
后悔当初 2020-12-01 11:59

I am working with the AWS SDK using the KMS libary. I would like to use async and await instead of callbacks.

import AWS, { KMS } from \"aws-sdk\";

this.kms         


        
2条回答
  •  生来不讨喜
    2020-12-01 12:20

    If you are using aws-sdk with version > 2.x, you can tranform a aws.Request to a promise with chain .promise() function. For your case:

      try {
        let key = await kms.generateDataKey().promise();
      } catch (e) {
        console.log(e);
      }
    

    the key is a KMS.Types.GenerateDataKeyResponse - the second param of callback(in callback style).

    The e is a AWSError - The first param of callback func

    note: await expression only allowed within an async function

提交回复
热议问题