0

COIN LIST is an array of crypto coins(["BTCUSDT",...]). I try to get the price using getPriceAction and RSI from getRSI and these two functions are working when I try to console DATA. But when I try to print the response after the completion of the loop. It prints the empty array and the length is 0 of this array. I want to store the DATA object (consisting of SYMBOL, closing price and RSI) as an element in the response array

 import { COIN_LIST } from "./COIN_LIST.js";
    import { getPriceAction } from "./PRICE_ACTION.js";
    import { getRSI } from "./RSI.js";

async function main() {
  try {
    let response = await [];
    await COIN_LIST.forEach((element, i) => {
      setTimeout(() => {
        let data = { symbol: element };
        getPriceAction(element, "4h").then((res) => {
          data.closingPrice = res;
          getRSI(res).then((res) => {
            data.RSI = res.reverse();
            data.closingPrice = data.closingPrice.reverse();
            response.push(data);
            console.log(data)
          });
        });
      }, i * 1000);
    });
    console.log(response);
  } catch (error) {
    console.log(error.message);
  }
}
main();
5
  • await COIN_LIST.forEach ... what makes you think forEach returns a Promise? Array forEach returns undefined - always - also ... let response = await []; ... huh? an Array is an Array, not a Promise - await documentation - I think you need to read how async/await works
    – Bravo
    Commented Apr 16, 2022 at 2:26
  • Is the COIN_LIST a Promise<Array>? If so, then you need to use (await COIN_LIST).forEach(...) Also, what do you mean by let response = await []? The array is not a promise in this line, so you should not use await in this line.
    – Tanay
    Commented Apr 16, 2022 at 2:36
  • @Bravo where do u think there is a problem Commented Apr 16, 2022 at 2:40
  • @Tanay COIN_LIST is a pre-defined array consisting of coins Commented Apr 16, 2022 at 2:41
  • @BasitQayoom you should not use await on COIN_LIST if it is a pre defined array
    – Tanay
    Commented Apr 16, 2022 at 2:47

2 Answers 2

0

If you want to use async/await properly for your code, then use async/await, don't use .then/.catch as well

Some notable changes

there is no setTimeout of increasing seconds ... just waiting 1 second after one result before getting the next - far cleaner, and if one request happens to take a lot longer, you won't end up with two requests at once (which may be an issue if the API is rate limited)

no .then ... use async/await OR .then/.catch - very rare to need both in the one function

don't use forEach with async/await ... it never does what you want, and creating an array of Promises inside a .forEach is extremely naive, you may as well use .map instead! then you can await Promise.all(xxx.map(.....)) - but that's useful for concurrent requests, not so much for serial requests like your code does

import { COIN_LIST } from "./COIN_LIST.js";
import { getPriceAction } from "./PRICE_ACTION.js";
import { getRSI } from "./RSI.js";

async function main() {
    try {
        const wait = (ms) => new Promise(resolve => setTimeout(resolve, 1000));
        let response = []; //---> don't need that `await`
        for (let element of COIN_LIST) {
            let data = { symbol: element };
            data.closingPrice = await getPriceAction(element, "4h");
            const res = await getRSI(data.closingPrice);
            data.RSI = res.reverse();
            data.closingPrice = data.closingPrice.reverse();
            response.push(data);
            console.log(data);
            await wait(1000);
        }
        console.log(response);
    } catch (error) {
        console.log(error.message);
    }
}
main();

the await wait(1000) could be tweaked depending on the rate limiting of the API ... if the rate limit applies to when the request is made, you could make a function that is smart about the delay between requests.

The code this way assumes the rate limit is based on the period between previous response to next request.

3
  • can I use await keyword for console.log(response) outside try block Commented Apr 16, 2022 at 3:05
  • @BasitQayoom - TL;DR ... no need to use await for any part of console.log(response); as there are no promises involved at all .... the long version: why would you await console.log? you still don't get what async/await is ... it's syntax sugar for Promise ... you only "need" to await a Promise, and only if you need to get its value at that point in the code - console.log anywhere after where it is shown in that code (but WITHIN that async function) will log correctly
    – Bravo
    Commented Apr 16, 2022 at 3:07
  • @BasitQayoom - I'm guessing that you have an issue trying to use the response outside of function main ... but this answer addresses the code you posted, not the code you have
    – Bravo
    Commented Apr 16, 2022 at 3:13
-1

After the completion of the loop, the promises didn't get resolved yet, that's why it print an empty array. One way to achieve what you need is using await for(...), or wait for all promises to be resolved, and then print the results.

import { COIN_LIST } from "./COIN_LIST.js";
import { getPriceAction } from "./PRICE_ACTION.js";
import { getRSI } from "./RSI.js";

async function main() {
  try {
    let response = []; //---> don't need that `await`
    const promises = []; //---> array of promises
    COIN_LIST.forEach((element, i) => {
      setTimeout(() => {
        let data = { symbol: element };
        const promise = getPriceAction(element, "4h").then((res) => {
          data.closingPrice = res;
          getRSI(res).then((res) => {
            data.RSI = res.reverse();
            data.closingPrice = data.closingPrice.reverse();
            response.push(data);
            console.log(data)
          });
        });
        promises.push(promise) //---> save the reference to a promise
      }, i * 1000);
    });
    await Promise.all(promises) //---> await for all promises to be resolved, then print the result
    console.log(response);
  } catch (error) {
    console.log(error.message);
  }
}
main();
2
  • why not use .map if you want an array of Promises for Promise.all? That would make far more sense
    – Bravo
    Commented Apr 16, 2022 at 3:04
  • @Bravo My intention was strictly to answer the question Why on printing I get value of null from API call in Loop instead of teaching how to use promises. I wouldn't implement it in that way. Commented Apr 16, 2022 at 14:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.