Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do double brackets mean in javascript and how to access them

Situation

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 

Question

What do the double brackets [[ ]] mean, and how do I retrieve the value of [[PromiseValue]].

like image 323
Jeff Avatar asked Mar 07 '15 15:03

Jeff


People also ask

What do double brackets mean in JavaScript?

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 ).

What are the double brackets?

Double brackets or [[]] in math refer to rounding off the value inside to its greatest integer less than or equal to the value.

Why do we use double brackets?

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.

What do double brackets mean in HTML?

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.


1 Answers

What's the stuff inside [[]]

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

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. As of right now, there is no way as they are not a part of the specification and will break across browsers - sorry.

So how do I get my value?

getDefinitions().then(function(defs){     //access them here }); 

But what if it returns an error? In prevision towards these cases, add the following at the end (and outside of) of your .then().

.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.

like image 118
Benjamin Gruenbaum Avatar answered Sep 19 '22 15:09

Benjamin Gruenbaum