2

This Question is intended to be a canonical Question/Answer; see What is a canonical question/answer, and what is their purpose?. Do not have the definitive Answer, here. In lieu of the Question having previously been asked and answered am posting the Question here. If the Question has already been answered, will delete the present Question.


The Promises/A+ specification at point-2 states

A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled.

At a comment at this Question return value from Promise OP asks

Right, but does not that take the value out of the promise, or should I accept that this is impossible from the browser?

to which responded

Hesitant to state what is "impossible".

followed by providing links to the specification, Promises, You're Missing the Point of Promises.


Is it impossible to get the [[PromiseValue]] from a Promise object, other than viewing the property value of the object at console.log(promiseObject), without using the Promise object's then method?

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    What is [[PromiseValue]]? Do you mean [[PromiseResult]]? See [Properties of Promise Instances](http://www.ecma-international.org/ecma-262/7.0/#sec-properties-of-promise-instances) – Oriol Dec 17 '16 at 17:52
  • 1
    @Oriol Not sure if the internal property; using the term "property", for lack of an accurate description of the "property", here; is implemented as `[[PromiseResult]]` at the browser you are trying at. At chromium 52, the property is implemented as `[[PromiseValue]]`. E.g., `console.log(Promise.resolve("abc"))`. – guest271314 Dec 17 '16 at 17:54
  • 2
    There's nothing to say how a promise stores the PromiseResult and that, I believe, is up to the imlementation. It could be stored as a publicly accessible property that an outside agent could directly access (assuming they had some other means of knowing the promise was already fulfilled). But, since there's no standard way of knowing when the promise is fulfilled other than `.then()` and the result is provided there, there seems to be no point in trying to access the value any other way. – jfriend00 Dec 17 '16 at 17:58
  • 4
    And, all other questions I've seen about trying to directly access the value of the promise are from people that don't understand the whole future timing issue of when the value is available and how you have to use `.then()` anyway to know when the value is available. So, if you're using `.then()` to know when it's available, you might as well just use the value when `.then()` provides it. – jfriend00 Dec 17 '16 at 18:00
  • 1
    @jfriend00 Yes, agree with you assessment. Cannot determine the motivation of the OP of the linked Question in asking whether it was "impossible" to get the `[[PromiseValue]]`, or even if the `[[PromiseStataus]]` is "resolved". The inquiry by the OP at the comment, as interpreted here, is whether it is "impossible" to get the `[[PromiseValue]]` "from the browser". – guest271314 Dec 17 '16 at 18:01
  • 1
    @Bergi Had not considered building chromium to expose the internal slots _"Can I access them? Not unless you make your own Chrome or V8 build."_ That would make it not "impossible" to get the values. – guest271314 Dec 17 '16 at 18:10
  • 1
    Using things defined in the standard, it IS impossible to get the value any other way other than `.then()` (the standard defines no other way to get it). Whether it is impossible to get it in a non-standard way depends entirely upon the implementation. An implementation could expose another (non-standard) way to get it. – jfriend00 Dec 17 '16 at 18:11
  • 1
    @jfriend00 Yes, if _you_ are the implementation, e.g., building chromium to expose the internal slots in contravention to the specification, the process to get the values would not be "impossible". Though, at the point of transforming the internal slots to being exposed, that would not be the same as finding a way to get the values when the values were intended to not be exposed. – guest271314 Dec 17 '16 at 18:13
  • 2
    Short answer: no it is not posible in the browser for the moment, check for [ES7 async await](http://rossboucher.com/await/#/) in the future, and also [learn how bad async await actually is](https://spion.github.io/posts/es7-async-await-step-in-the-wrong-direction.html) and why it might not be implemented in the browsers. By the way why would you try to make a yes/no question cannonical? – Marco Scabbiolo Dec 17 '16 at 18:15
  • 1
    I'm not sure why you're making this about Chromium? There are many implementations of promises one could use. For example, you could be using Bluebird or Q or a modified version of one of those. But, attempts or thoughts to retrieve the value directly as usually misguided anyway because of the uncertainty of timing which `.then()` is needed to resolve anyway. – jfriend00 Dec 17 '16 at 18:15
  • 1
    @jfriend00 Not making it about chromium. That is just one possibility. Of course, an individual could compose their own Promise implementation. The present inquiry is, without exceeding the parameters of what was actually written at the linked comment, whether it was "impossible" to get the value a a browser which had not expressly exposed the value. The inquiry does not intend to evaluate whether or not getting the value in such a manner is a sound decision. – guest271314 Dec 17 '16 at 18:16
  • 2
    The summary is that there is no standard way to directly retrieve `[[PromiseValue]]` and questions asking about that usually result from a misunderstanding of timing anyway. Whether or not there is a non-standard way depends entirely upon the implementation and whether it has offered something in this regard to standard Javascript that is outside the Promise standard. So, whether it is impossible depends upon the Promise implementation. – jfriend00 Dec 17 '16 at 18:19
  • 2
    @Bergi Not sure why that Question did not show up when was creating title of the Question. Included `[[PromiseValue]]` for that purpose, to avoid creating a duplicate Question. Closed as duplicate of the link you shared. – guest271314 Dec 17 '16 at 18:21
  • 1
    @MarcoScabbiolo Fair point. In case the question arose again. As this Question did. Are canonical Question/Answers limited to other than yes/no answers? – guest271314 Dec 17 '16 at 18:27
  • 1
    @Bergi Had read the duplicate Question previously. Should have searched more thoroughly before posting this Question. After all, actually posted a comment at the duplicate Question. It was erroneous to post this Question. – guest271314 Dec 17 '16 at 18:35

1 Answers1

2

Not possible in ES7. The value of a promise is stored in the [[PromiseResult]] internal slot:

The value with which the promise has been fulfilled or rejected, if any. Only meaningful if [[PromiseState]] is not "pending".

That slot is only read in the PerformPromiseThen abstract operation, which is only exposed via Promise.prototype.then.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Just occurred that if `console.log(Promise.resolve("a value"))` prints the internal slot of the `Promise` to `console`, could the result of a `console.log()` call be retrieved, if only as text? – guest271314 Dec 17 '16 at 18:05
  • @guest271314 Maybe if you have privileged code like an extension, otherwise I don't think so. – Oriol Dec 17 '16 at 18:09