I have the following function, attempting to use Promises.
var getDefinitions = function(){
return new Promise(function(resolve){
resolve(ContactManager.request("definition:entities"));
});
}
var definitions = getDefinitions()
is returning:
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: child}
I want to get the value of PromiseValue, but asking for
var value = definitions.PromiseValue
gives me an undefined result.
My question is what do the double brackets [[ ]]
mean, and how do I retrieve the value of [[PromiseValue]]
.
This example is with react but for the most part it should be the same.
Replace this.props.url with your url to fetch to make it work for most other frameworks.
Parsing the res.json() returns the [[promiseValue]] however if you then return it to another .then() method below you can return it as a total array.
I think that it will go well with this.
Reading the manpage, we can see that:
Emphasis mine. Therefore, what you want to do cannot be done. The better question is why do you need to access the promise state like that?
What's the stuff inside
[[]]
It's an internal property. You cannot access it directly. Native promises may only be unwrapped in
then
with promises or asynchronously in generally - see How to return the response from an asynchronous call. Quoting the specification:You cannot
Seriously though - what are they?
Very nice! As the above quote says they're just used in the spec - so there is no reason for them to really appear in your console.
Don't tell anyone but these are really private symbols. The reason they exist is for other internal methods to be able to access
[[PromiseValue]]
. For example when io.js decides to return promises instead of taking callbacks - these would allow it to access these properties fast in cases it is guaranteed. They are not exposed to the outside.Can I access them?
Not unless you make your own Chrome or V8 build. Maybe in ES7 with access modifiers, right now there is no way as they are not a part of the specification and will break across browsers - sorry.
So I do I get my value?
Although if I had to guess - you're not converting the API correctly to begin with since this conversion would only work in case the method is synchronous (in that case don't return a promise) or it returns a promise already which will make it resolved (which means you don't need the conversion at all - just
return
.I also walked into this problem today and happened to find a solution.
My solution looks like this:
Here,
dataWrappedByPromise
is aPromise
instance. To access the data in thePromise
instance, I found that I just needed to unwrap that instance with the.json()
method.Hope that helps!