How to use Async and Await with AWS SDK Javascript

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

Leave a Comment