Use a promise result from a fetch-api in an if statement of another function – JS Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Use a promise result from a fetch-api in an if statement of another function – JS without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So I have this fetch-api

let test = () => fetch(usd_api).then(response => response.json())
.then(data => data.exchange_rates.dash_usd);

Console log

let console_test = () => {console.log(test())}

enter image description here

How can use this number [[PromiseResult]]: 134.445... in the following function where the number 150 is.

function main(){

         fetch(txs_api).then(response => response.json()).then(function(data) {
                
             var amount = data.txs[0].vout[1].value;
               
             if(amount == 150){
                // SUCCESS!
                $('#modal').modal('hide');
                console.log('requests stopped');
                clearInterval(interval);
             }
         })
}

Answer

I figured it out. Don’t know if it’s the best way to do it but it’s working.

function main(){

  fetch(usd_api).then(response => response.json()).then(function(data) {
                        
      var dash_price = data.exchange_rates.dash_usd;
                    
      fetch(txs_api).then(response => response.json()).then(function(data) {
                    
          var amount = data.txs[0].vout[1].value;

          if(amount == dash_price){
              // SUCCESS!
              $('#modal').modal('hide');
              console.log('requests stopped');
              clearInterval(interval);
          }
      })
   })
}
We are here to answer your question about Use a promise result from a fetch-api in an if statement of another function – JS - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji