Warning: I'm going to sound like I have no idea what I'm talking about here, because I kind of don't. I'm in the process of self-learning Javascript and AngularJS through a lot of trial and error coding.
I have some javascript code (hesitant to copy here because it's a mess) that returns an Object with the following structure:
What I want to save to a variable is the object corresponding to Object.$$state.value in the picture. This object has username, hash and salt, which are what I care about. I don't know what all the other stuff like $$state are or how they got there.
However, if I do this (let's call the main Object "whatIHave"):
var whatIWant = whatIHave.$$state.value;
this does not work. whatIWant is null.
Does anyone recognize what's going on here? What is $$state, how did it get there, and how can I extract the value I want?
So that is a promise. You need to do something like:
whatIHave.then(function(whatIWant) {
// Work here
});
I highly recommend you to research what a promise is (like this link)
If you're curious enough, about what is that $$state
and what is that value
, I will explain a bit:
The promises have a $$state
and there angular saves all the callback functions you want to call in a pending
array (all those function you registered with .then
like I explained before).
It also have the status: resolved (1) and rejected (2)
Finally, when you resolve
or reject
the promise, the value you pass when doing that, is saved in value
.
You're trying to cheat in here, because when you try to access that value
, it could be not there yet (that is what async
is all about).
So the idea is learning the basic of promises, learning how to work with them and then use your whatIHave
correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With