简体   繁体   中英

Using global variables in node js in different async functions

I'm trying to get more familiar with best practices in NodeJS. Currently, I have a asynchronous function that scrapes some data off a website and stores the values retrieved in an object. What I would like to do, is use the value in a different function to extract data from Yahoo Finance to retrieve specific values. I am unsure how to pass in this value to the other functions. I'm thinking possibly setting the value, that is passed in to to other functions, as a global variable. Would that be best practice in the NodeJS world of programming? Any opinions or advice would helpful. Below is the code I currently have:

const cheerio = require('cheerio');
const axios = require("axios");
 
async function read_fortune_500() {
  try {
    const { data } = await axios({ method: "GET", url: "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies", })
    const $ = cheerio.load(data)
    const elemSelector = '#constituents > tbody > tr > td:nth-child(1)'
    const keys = ['symbol']

    $(elemSelector).each((parentIndex, parentElem) => {
      let keyIndex = 0
      const stockObject = {}
      if (parentIndex <= 9){
      $(parentElem).children().each((childIndex, childElem) => {
        const tdValue = $(childElem).text()

        if (tdValue) {
          stockObject[keys[keyIndex]] = tdValue
        }
      })
      console.log(stockObject)
    }
    })
} catch (err) {
    console.error(err)
  }
}

async function getCurrentPrice() {}

read_fortune_500()

Sounds more like a JavaScript question then a NodeJS specific question.

NodeJS: I would say you can store the result of scraping the website in session data. Or pass it along in the response and call next(). Or create some middleware to scrape the website before calling the Yahoo route.

Javascript: You can call a async function to scrape the data on your site and await the response. once it is done you can call your next function passing the data retrieved from async result. See below for basic example.

 async function scrapeWebsite(){ let webScrapeReults; // logic to scrape site return webScrapeReults; } function getYahooMarket(){ let results; let webData = await scrapeWebsite(); // use webData to get reults for yahooMarket return results; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM