4

I am using a Promise function to facilitate an ajax call. When I console log the 'value.bal' value from the server within the then function the expected value shows up in the console, but when I return it console.log it outside the then function the result is 'object Promise'. What am I missing?

    var promiseObject = SomefullfilledPromise;
    var result = promiseObject.then(function(value){
       console.log(value.bal); //expected value
       return value.bal;
    });
    console.log(result); //'object Promise'
SuperCatchyUsername
  • 375
  • 1
  • 5
  • 14

1 Answers1

4
var result = promiseObject.then(function(value){
       console.log(value.bal); //expected value
       return value.bal;
    });

would return a promise you can access the result of this promise in a .then()

.then((val) => {
    console.log(val)
})
marvel308
  • 10,288
  • 1
  • 21
  • 32
  • 1
    Thank you. That just repeats the ".then" portion of what I have. The problem is I can't get the "value.bal" piece out of the promise object to use in the rest of my code. – SuperCatchyUsername Sep 21 '17 at 13:44
  • @AppreciateCo it's because that's not what you are supposed to do – Maya Sep 21 '17 at 13:45
  • you cannot access asynchronous code synchronously, you would have to resolve the variable or pass it in a callback – marvel308 Sep 21 '17 at 13:45
  • 1
    So I have to do whatever I need to do with the result form within the 'then' function. I get it. These function are tricky little guys. Thank you! – SuperCatchyUsername Sep 21 '17 at 13:49