Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I promisify the AWS JavaScript SDK?

I want to use the aws-sdk in JavaScript using promises.

Instead of the default callback style:

dynamodb.getItem(params, function(err, data) {   if (err) console.log(err, err.stack); // an error occurred   else     console.log(data);           // successful response }); 

I instead want to use a promise style:

dynamoDb.putItemAsync(params).then(function(data) {   console.log(data);           // successful response }).catch(function(error) {   console.log(err, err.stack); // an error occurred }); 
like image 498
Martin Kretz Avatar asked Oct 20 '14 21:10

Martin Kretz


People also ask

How do I connect to AWS SDK?

To install the this package, simply type add or install @aws-sdk/client-connect using your favorite package manager: npm install @aws-sdk/client-connect. yarn add @aws-sdk/client-connect. pnpm add @aws-sdk/client-connect.

Which command may be used to import the AWS SDK for JavaScript into a JavaScript module?

The preferred way to install the AWS SDK for JavaScript for Node. js is to use npm, the Node. js package manager . To do so, type this at the command line.

How do I integrate the latest version of the AWS SDK for JavaScript into my node JS lambda function using layers?

To integrate the latest version of an AWS SDK into your Lambda function's deployment package, create a Lambda layer, and then add it to your function. You can use either the AWS Command Line Interface (AWS CLI) or the Lambda console to create a Lambda layer and add it to your function.


2 Answers

The 2.3.0 release of the AWS JavaScript SDK added support for promises: http://aws.amazon.com/releasenotes/8589740860839559

like image 154
Majix Avatar answered Sep 19 '22 07:09

Majix


I believe calls can now be appended with .promise() to promisify the given method.

You can see it start being introduced in 2.6.12 https://github.com/aws/aws-sdk-js/blob/master/CHANGELOG.md#2612

You can see an example of it's use in AWS' blog https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/

let AWS = require('aws-sdk'); let lambda = new AWS.Lambda();  exports.handler = async (event) => {     return await lambda.getAccountSettings().promise() ; }; 
like image 37
DefionsCode Avatar answered Sep 19 '22 07:09

DefionsCode