Last active
June 20, 2022 02:52
-
-
Save rxgx/7e1b24de5936ff1b2b815a3d9cc3897a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const client = new AWS.SecretsManager({}); | |
// Call the AWS API and return a Promise | |
function getAwsSecret(secretName) { | |
return client.getSecretValue({ SecretId: secretName }).promise(); | |
} | |
// Create a async function to use the Promise | |
// Top level await is a proposal | |
async function getAwsSecretAsync (secretName) { | |
var error; | |
var response = await getAwsSecret(secretName).catch(err => (error = err)); | |
return [error, response]; | |
} | |
// Call the async function and return NodeJS callback style | |
module.exports = function asyncExample () { | |
var [error, secret] = getAwsSecretAsync('dev/MySecret/MyService'); | |
if (error) { | |
// Trigger an error and halt | |
console.error(error); | |
return; | |
} | |
// Use the result | |
console.debug(secret); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Top level
async
/await
is now available in Node.js LTS (v16.15.0 as of now).