I have the following function which uses Promise.
var getDefinitions = function() { return new Promise(function(resolve) { resolve(ContactManager.request("definition:entities")); }); } var definitions = getDefinitions()
The contents of definitions
is:
Promise { [[PromiseStatus]]: "resolved", [[PromiseValue]]: child }
Accessing the PromiseValue
property directly returns undefined
var value = definitions.PromiseValue; // undefined
What do the double brackets [[ ]]
mean, and how do I retrieve the value of [[PromiseValue]]
.
JavaScript [[Prototype]] The double bracket [[Prototype]] is an internal linkage that ties one object to another. When creating a function, a property object called prototype is being created and added to the function's name variable (that we call constructor ).
Double brackets or [[]] in math refer to rounding off the value inside to its greatest integer less than or equal to the value.
2.2. Double Brackets. The double brackets, [[ ]], were introduced in the Korn Shell as an enhancement that makes it easier to use in tests in shell scripts. We can think of it as a convenient alternative to single brackets.
The double curly brackets are not HTML but scripting code. The term inside, interest, is a placeholder, sort of like the name and address in a form letter. The string {{interest}} will be replaced when the HTML template is converted into straight HTML that is sent over the network to the user.
[[]]
My question is what do the double brackets [[ ]] mean, and how do I retrieve the value of [[PromiseValue]].
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:
They are defined by this specification purely for expository purposes. An implementation of ECMAScript must behave as if it produced and operated upon internal properties in the manner described here. The names of internal properties are enclosed in double square brackets [[ ]]. When an algorithm uses an internal property of an object and the object does not implement the indicated internal property, a TypeError exception is thrown.
You cannot
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.
Not unless you make your own Chrome or V8 build. Maybe in ES7 with access modifiers. As of right now, there is no way as they are not a part of the specification and will break across browsers - sorry.
getDefinitions().then(function(defs){ //access them here });
.catch(function(defs){ //access them here });
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
.
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