0

I have a problem with my project. I'm trying to set API request and once it done I want to store the res in var.

My code:

function getMyBtcBalace(){
return new Promise((resolve, reject) =>{
  bit2c.getBalance(credentials, function(error, balance)  {
     resolve(balance.BTC);
     //return
  })
 });
}

///After API call
var temp = getMyBtcBalace();

In my project I want to get the updatest value of balace.BTC every time I call getMyBtcBalace function.

As you can see I use Promise, but I don't want continue coding in then() function.

Example:

 function AsyncFunc(){
  ///// Async
  }

function getMyBtcBalace(){
return new Promise((resolve, reject) =>{
  bit2c.getBalance(credentials, function(error, balance)  {
     resolve(balance.BTC);
     //return
  })
 });
}

while (getMyBtcBalace() > AsyncFunc2())
{
  /// Do something 
}
DD apps
  • 57
  • 1
  • 6
  • Because I don't want to continue coding in then(), I just want to call getMyBtcBalace and to get an answer! – DD apps Sep 05 '17 at 07:10
  • You can't continue coding outside the `.then()`. That's how asynchronous operations work in Javascript. The only place you can use the async result is INSIDE the `.then()` handler or in some function you call from there and pass it the result. See the question yours has been marked a duplicate of for lots more explanation and coding options. – jfriend00 Sep 05 '17 at 07:12
  • So want you're saying is that I can't work outsite the then() function, so how I can do the while loop. – DD apps Sep 05 '17 at 07:14
  • Just to make things clear, I want to call 5 different API requests and use it. So I can\t work in one then() function because I have 4 more to deal with – DD apps Sep 05 '17 at 07:16
  • I just edited my Q , please see :-) – DD apps Sep 05 '17 at 07:19

0 Answers0