1

I would like main() to return the data instead of just console logging it as shown below. How do I do it?

async function main() {
  // Load the AWS SDK for Node.js
  var AWS = require("aws-sdk");
  // Set the region
  AWS.config.update({ region: "REGION" });

  // Create S3 service object
  s3 = new AWS.S3({ apiVersion: "2006-03-01" });

  // Call S3 to list the buckets
  s3.listBuckets(function (err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      console.log(data);
    }
  });
}
main();
Gerald
  • 45
  • 8

1 Answers1

1

Try awaiting the listBuckets function.

Change

s3.listBuckets(function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log(data);
  }
});

to

return await s3.listBuckets();

If that doesn't work, you can always just construct a manual Promise and resolve it with the data.

async function main() {
  // Load the AWS SDK for Node.js
  var AWS = require("aws-sdk");
  // Set the region
  AWS.config.update({ region: "REGION" });

  // Create S3 service object
  s3 = new AWS.S3({ apiVersion: "2006-03-01" });

  return await new Promise((resolve, reject) => {
    // Call S3 to list the buckets
    s3.listBuckets(function (err, data) {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}
main();
skara9
  • 4,042
  • 1
  • 6
  • 21