Save response of GET request

Tell us what’s happening:

I’m using the request module to access the FCC API for stock prices. I created a custom function that accesses the API to get the latest price for the stock. I am able to console.log the price but can’t find anyway of using it. I tried returning it from my function (it’s undefined, or I can return the request object) or updating a global variable as suggested here (doesn’t work).

How do I change this function to make it return latestPrice?

Your code so far

function getPrice(name) {
  request(
    "https://repeated-alpaca.glitch.me/v1/stock/" + name + "/quote",
    { json: true },
    (err, res, data) => {
      if (err) {
        console.log(err);
        return;
      };
      price = data.latestPrice;
      console.log(data.latestPrice);
    }
  );
};

getPrice('goog');

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36.

Challenge: Stock Price Checker

Link to the challenge:
https://www.freecodecamp.org/learn/information-security-and-quality-assurance/information-security-and-quality-assurance-projects/stock-price-checker