title: This method in nodejs is returning an undefined object
body: <p>I asked this question earlier and it got associated with this question ( <a href="https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call">How do I return the response from an asynchronous call?</a> ) But It doesn't resolve my problem</p>
  
  <p>i am very new to node.js and i'm having some error that i can't deal with or find any help that i understand. I used express-generator to initialize the app. I'm trying to send and object i get through an api call to the front end. I wrote a class "StockClass.js" with a function makeApiCall() which makes the call and returns the object. But when i call that function in the router i get "undefined". Heres the code</p>
  
  <pre><code>//==== StockClass.js ===
  
  const yahooFinance = require("yahoo-finance");
  class stockClass {
      static makeApiCall(symbol) {
          yahooFinance.quote(
              {
                  symbols: [symbol],
                  modules: ["price", "summaryDetail"],
              },
              function (err, quotes) {
                  if (err) { console.log(err) }
                  console.log(quotes)
                  return quotes;
              }
          );
      }
  }
  
  module.exports = stockClass;
  
  //=====index.js======
  
  const StockClass = require("../handlers/StockClass");
  
  router.get("/new", function (req, res) {
  
     let quotes = StockClass.makeApiCall("AAPL");
     console.log(quotes);
     res.render('path', { quotes });
  });
  </code></pre>
  
  <p>The console.log in the StockClass.js logs the object (quotes) correctly while the console.log in index.js logs "undefined".</p>
  
  <p>Link below explains the yahoo-finance api call. <a href="https://www.npmjs.com/package/yahoo-finance" rel="nofollow noreferrer">https://www.npmjs.com/package/yahoo-finance</a></p>
  
  <p>==========================================================================</p>
  
  <p>I also tried using a middleware and attaching the data to the response object like this</p>
  
  <pre><code>//==========middleware=========
  
  const yahooFinance = require("yahoo-finance");
  
  module.exports = {
  
      makeApiCall: (symbol) =&gt; {
          return function (req, res, next) {
              yahooFinance.quote(
                  {
                      symbols: [symbol],
                      modules: ["price", "summaryDetail"],
                  },
                  function (err, quotes) {
                      if (err) { console.log(err) }
                      res.stockObj = quotes;
                      console.log(res.stockObj);
                  }
              );
              next();
          }
      }
  }
  
  //======= index.js =========
  const handler = require("./handlers/stockUtils");
  
      router.get("/new", handler.makeApiCall("AAPL"), function (req, res) {
  
         let quotes = res.stockObj;
         console.log(quotes);
         res.render('path', { quotes });
      });
  </code></pre>
  
  <p>And the results are the same. The console.log in the middleware function logs the correct object, but the console.log in index.js logs undefined</p>