Convert Callback Functions Into Promises With NodeJS Promisify Package
Meddy369 opened this issue · 1 comments
Meddy369 commented
I am working on a small program using the aws-cli-js, and would like to use async and await instead of the callback based method. I have the following code:
// import packages cli and promisify
const awsCli = require('aws-cli-js');
const { promisify } = require('util');
// configure aws-cli-js
const Options = awsCli.Options;
const Aws = awsCli.Aws;
const options = new Options(accessKey, secretKey);
const aws = new Aws(options);
// promisifiy aws.command function
const awsCommandProm = promisify(aws.command);
async function getLambdas(){
try {
const response = await awsCommandProm('lambda list-functions');
console.log(response)
} catch (error) {
console.error(error);
}
}
const lambdaFunctions = getLambdas();
When the code executes the following error is thrown: TypeError: Cannot read properties of undefined (reading 'options') at command (/pathTo/node_modules/aws-cli-js/lib/index.js:49:37)
I used the promisify function from the node.js util package and know that it takes a function that is callback based and converts is to a promise based function. What am I doing wrong here?
mattqs commented
See https://github.com/Quobject/aws-cli-js#usage
Usage
With promise
var options = new Options(
/* accessKey */ 'your key',
/* secretKey */ 'your key2',
/* sessionToken */ 'your token',
/* currentWorkingDirectory */ null,
/* cliPath */ 'aws'
);
var aws = new Aws(options);
aws.command('iam list-users').then(function (data) {
console.log('data = ', data);
});
//i.e.
var data = await aws.command('iam list-users');