Forbes
Forbes

Reputation: 1

Access return value in AWS Lambda function

const https = require("https");
let url = "";

exports.handler = async function (event) {
  let rawData = "";
  const promise = new Promise(function (resolve, reject) {
    https
      .get(url, (res) => {
        res.on("data", (chunk) => {
          rawData += chunk;
        });
        res.on("end", function () {
          resolve(rawData);
        });
      })
      .on("error", (e) => {
        reject(Error(e));
      });
  });
  return promise;
};

How do I access the value of promise and console.log it?

I want to log the value of promise.

Upvotes: 0

Views: 121

Answers (1)

emilio
emilio

Reputation: 762

As your function is async you need to await the promise before returning to be able to log it within your function.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises#async_and_await

Upvotes: 1

Related Questions