Promise chaining... I need help

So yeah, I’m working on a project I think I’ve encountered a problem… Is there a much better way to clean this up. Everything works as it is but I think I remember learning about something for this specific situation?

app.get('/Digitalhome',(req, res)=>{
    // All live prices of crypto
    cc.price('BTC', 'USD').then(btc => { //bitcoin
        cc.price('LTC', 'USD').then(ltc => { //litecoin
            cc.price('ETH', 'USD').then(eth => { //ethereum
                cc.price('EOS', 'USD').then(eos => { // eos
                    cc.price('USDT', 'USD').then(usdt => { //tether
                            bitCoin: btc.USD,
                            liteCoin: ltc.USD,
                            ethereum: eth.USD,
                            eos: eos.USD,
                            tether: esdt.USD

I remember learning about promise chaining Is this an example of when that can be used?

Promise.all. This code says you only want the prices to be rendered if all the prices can be resolved, so you should use that (if that isn’t what you want, then you’re making each request depend on the previous one succeeding).

1 Like

You could chain your promises here but it looks like there is no reason to because the next promise doesn’t rely on any data from the previous promise.

Right now all the price checks are happening sequentially, chaining the promises will do the same thing but with a cleaner syntax to avoid the callback hell style.

Instead of getting all the values sequentially like you are doing here you do it concurrently, calling each price check at the same time instead of waiting for the previous to finish first.

Promise.all() is one way to accomplish this.

You should get intimately familiar with all the promises docs and asynchronous code because that is a huge part of node and JS in general.

Once you master promises you have the option to use the new async/await style as well.

1 Like

Yeah, I’m begining to notice that.
I’m not going to lie I’ve been somewhat skimming the whole promise thing because I find it so confusing. I’ll just have to start filling the gaps in my knowledge about promises.

Okay so this was my solution

let p1 = cc.price('BTC', 'USD')//bitcoin
    let p2 = cc.price('LTC', 'USD')//litecoin
    let p3 = cc.price('ETH', 'USD')//ethereum
    let p4 = cc.price('EOS', 'USD')// eos
    let p5 = cc.price('USDT', 'USD')//tether

    Promise.all([p1, p2, p3, p4, p5]).then((priceArr)=>{
            bitCoin: priceArr[0].USD + " USD",
            liteCoin: priceArr[1].USD + " USD",
            ethereum: priceArr[2].USD + " USD",
            eos: priceArr[3].USD + " USD",
            tether: priceArr[4].USD + " USD"

Does that look better? The page loads soooo much faster.

That reads very nicely indeed!

@DanCouper Sorry to hijack this thread, but I was curious if the following or something like the following would work?

app.get('/DigitalHome', async (req, res) => {
  const usConversions = {};
  const currencies = [
    ['bitCoin','BTC'], ['liteCoin','LTC'], ['ethereum','ETH'],
    ['eos','EOS'], ['tether','USDT']
  for ([ name, type ] of currencies) {
    let { USD } = await someFunction(type, 'USD');
    usConversions[name] = USD;
  res.render('digitalHome', usConversions);

Would the catch work properly? Where would a catch go?