I have a number of stock tickers in a JSON file that I want to use to send multiple GET requests to get the price of that stock. The problem I'm having is how to send them off in parallel and how to resolve them.
Here is the code I have tried:
const stocks = require('./stocks.json')
var request = require("request");
var stockTickers = stocks.map(x => x.stockopediaTicker)
stockTickers.forEach(ticker => {
var promises = []
var options = {
method: 'GET',
url: 'https://www.stockopedia.com/ajax/get_prices/' + ticker + '/'
};
let todaysQuote = new Promise(function (res, rej) {
request(options, function (error, response, body) {
rej(error)
res(body)
});
})
promises.push(todaysQuote)
});
Promise.all(promises)
.then((results) => {
console.log(results)
}).catch(err => console.log(err))
var promises = []is defined in a scope thatPromise.alldoesn't have access to. Try definingpromisesoutside theforEach