9

If you know that the Promise has already been resolved why can't you just call get() on it and receive the value? As opposed to using then(..) with a callback function.

So instead of doing:

promise.then(function(value) {
  // do something with value
});

I want to be able to do the much simpler:

var value = promise.get();

Java offers this for it's CompletableFuture and I see no reason why JavaScript couldn't offer the same.

Roland
  • 7,525
  • 13
  • 61
  • 124
  • It's because it's not in the standard. Are you asking for an alternative way to obtain the status of the Promise? – amphetamachine Jul 28 '17 at 16:25
  • 4
    @amphetamachine not the status, I want the resulting value of the Promise, assuming the promise is already resolved. – Roland Jul 28 '17 at 16:27
  • @Roland Because it would have to throw an exception if the promise was *not* fulfilled. Also, you never really need this. There's always the save way of using `then` – Bergi Jul 28 '17 at 16:30
  • 2
    `async..await` makes this feature largely unnecessary – zzzzBov Jul 28 '17 at 16:32
  • Related: [How can I synchronously determine a JavaScript Promise's state?](https://stackoverflow.com/q/30564053/1048572) – Bergi Jul 28 '17 at 16:32
  • @Bergi True, you can do it with `then(..)` but the point is to have a simpler alternative when you know the promise is resolved. – Roland Jul 28 '17 at 16:32
  • 2
    _"it could also block"_. No, it can't. – robertklep Jul 28 '17 at 16:33
  • you can't block and wait because blocking will prevent the promise from ever resolving. – zzzzBov Jul 28 '17 at 16:33
  • @Roland There's your fallacy. It's not a simpler alternative, and "knowing" whether the promise is resolved is something easy to mess up. – Bergi Jul 28 '17 at 16:42
  • 1
    If you want to hack something so you can access the data without using `.then()`, then just create your own `.then()` handler and stuff the result anywhere you want and access it there (in your own variable, in your own property on the promise, etc...). This is not how promises work for lots of good reasons, but you can create your own hack however you want (it would probably be flawed design, but you can still do it). Promises guarantee you will get result or error with `.then() ` or `.catch()`. Accessing a variable directly cannot do either. – jfriend00 Jul 28 '17 at 16:46
  • Does this answer your question? [How to access the value of a promise?](https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise) – Liam May 10 '22 at 08:20
  • @Liam No, it does not. – Suncat2000 Jun 14 '22 at 14:00

2 Answers2

10

Java's get method "Waits if necessary for this future to complete", i.e. it blocks the current thread. We absolutely never want to do that in JavaScript, which has only one "thread".

It would have been possible to integrate methods in the API to determine synchronously whether and with what results the promise completed, but it's a good thing they didn't. Having only one single method, then, to get results when they are available, makes things a lot easier, safer and more consistent. There's no benefit in writing your own if-pending-then-this-else-that logic, it only opens up possibilities for mistakes. Asynchrony is hard.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Ok, so throw an exception or return `undefined` if the `Promise` is not resolved yet. – Roland Jul 28 '17 at 16:38
  • 1
    @Roland Which programmer would catch these exceptions? Be realistic. And there's really no use case for such a `get` method. – Bergi Jul 28 '17 at 16:40
  • 5
    @Bergi The use case is in the REPL when you have a promise that's been resolved, and you need to get the value to play around with it. – Resigned June 2023 Jul 28 '17 at 22:06
  • @RadonRosborough I see. REPL is quite an edge case though, and even there you have to wait for the promise to resolve - and you don't want to have to input a second line after waiting. The workaround is a rather simple `.then(x=>console.log(_=x),console.error)`, and also [there's a feature request to use `await`](https://github.com/nodejs/node/issues/13209). – Bergi Jul 28 '17 at 22:51
-1

Of course it not, because the task will run asynchronously so you can't get result immediately.

But you can use a sync/await to write sequential asynchronous code.

Li Jinyao
  • 898
  • 2
  • 12
  • 30