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 });
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.
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.
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.
The 2.3.0 release of the AWS JavaScript SDK added support for promises: http://aws.amazon.com/releasenotes/8589740860839559
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() ; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With