0

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))
1
  • var promises = [] is defined in a scope that Promise.all doesn't have access to. Try defining promises outside the forEach Commented Jun 14, 2019 at 18:10

3 Answers 3

1

You were on the right path with your code.

  1. You just need to only call rej(err) if there is actually an error
  2. You shouldn't be attempting to share the same options object with all your requests (that might or might not cause problems)
  3. You need to declare promises in a higher scope where it can be used with Promise.all(promises):

Here's what it would look like after fixing up those issues:

const stocks = require('./stocks.json')
const request = require("request");

let promises = [];
let stockTickers = stocks.map(x => x.stockopediaTicker);

stockTickers.forEach(ticker => {
    let todaysQuote = new Promise(function (res, rej) {
        let options = {
            method: 'GET',
            url: 'https://www.stockopedia.com/ajax/get_prices/' + ticker + '/'
        };
        request(options, function (error, response, body) {
            if (error) {
                rej(error);
            } else {
                res(body);
            }
        });
    })
    promises.push(todaysQuote)
});

Promise.all(promises)
    .then((results) => {
        console.log(results);
    }).catch(err => console.log(err))

The request-promise module is simpler to use as it already includes a promise wrapper and it's probably easier to use .map() to accumulate your array of promises.

const stocks = require('./stocks.json')
const rp = require("request-promise");

Promise.all(stocks.map(x => {
    let options = {
        method: 'GET',
        url: 'https://www.stockopedia.com/ajax/get_prices/' + x.stockopediaTicker + '/'
    };
    return rp(options);
})).then(results => {
    console.log(results);
}).catch(err => {
    console.log(err);
});
Sign up to request clarification or add additional context in comments.

Comments

0

You should check if there is an error and only rej if there is an error:

let todaysQuote = new Promise(function (res, rej) {
        request(options, function (error, response, body) {
            if(error) {
                return rej(error)
            }

            res(body)
        });
    })

Right now, you're "rejecting" every response.

1 Comment

I was under the impression that a promise was only rejected if request raised an error?
0

I'm not sure about "request" but using "request-promise-native" you can simplify some things like this.

const stocks = require('./stocks.json');
const request = require('request-promise-native');

const parseStocks = (stocks)=>Promise.all(stocks.map(requestQuote));

const requestQuote = ({stockopediaTicker})=>{
    const options = {
        method: 'GET',
        url: `https://www.stockopedia.com/ajax/get_prices/${stockopediaTicker}/`
    };
    return request(options)
}

parseStocks(stocks).then(console.log).catch(console.log)

Normally I refrain from suggesting dependency changes in an answer, but in this case, 'request-promise-native' is suggested by the 'request' docs. If you plan on using promises you may want to switch. It's usually best practice to avoid combining callbacks and promise chains together.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.