Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Get promise value inside loop

I'm a beginner in Javascript/NodeJS and I'm prefer practical work, I'm practicing by creating a bot discord with discord.js

The objective of this one is simply to display me the current value of the bitcoin in USD.

To get the value of BTC, I use the coingecko-api package.

However I'm blocked because although I can get the value of bitcoin, I can't do it when I'm in a loop without really understanding why.

main.js :

var coin = require('./coingecko');

while(true) {
  coin.btc().then((value => console.log (value) )); // <= This show nothing

  // Other things
  ...
}

coingecko.js :

const CoinGecko = require('coingecko-api');
const CoinGeckoClient = new CoinGecko();

module.exports = {
    btc: async function btc() {
      let btc = await CoinGeckoClient.coins.fetch('bitcoin', {});

      return btc['data']['market_data']['current_price']['usd'];
    }
}

Any tips ? Thank's

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Put your loop into an async function. Using await rather than .then() makes your looping logic much more straightforward.

const coin = require('./coingecko');

/* declare the async function */
async function doLoop () {
  while(true) {
    const value = await coin.btc()
    console.log(value)
    // Other things
    ...
  }
}

/* invoke the async function from non-async context */
doLoop().catch(console.error)
about 4 years ago · Santiago Trujillo Report

0

Are you sure about the ["data"] part?

When I test this API here I get this response: https://www.coingecko.com/en/api/documentation

"market_data": {
"current_price": {
  "aed": 183098,
  "ars": 5097411,
  ...
  
  "uah": 1356629,
  "usd": 49850,

Also you can probably just do btc.market_data.current_price.usd

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!