How to use Async and Await with AWS SDK Javascript

前端 未结 2 1332
后悔当初
后悔当初 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:07

    await requires a Promise. generateDataKey() returns a AWS.Request, not a Promise. AWS.Request are EventEmitters (more or less) but have a promise method that you can use.

    import AWS, {
      KMS
    } from "aws-sdk";
    
    (async function() {
      const kms = new AWS.KMS();
      const keyReq = kms.generateDataKey()
      const key = await keyReq.promise();
    
      // Or just:
      // const key = await kms.generateDataKey().promise()
    }());
    

提交回复
热议问题