Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of resolved Promise in sync

If we know that a Promise is definitely resolved, how can we access the value and if we can't, why not?

let a = Promise.resolve(123);

console.log(a.value); // ???

The following does not work- it prints "First, Last, 123"

console.log("First");
Promise.resolve(123).then(console.log);
console.log("Last");

I'm asking how to get the value of an already resolved Promise synchronously and if that's not possible, why not?

like image 823
Lolums Avatar asked Nov 22 '18 14:11

Lolums


People also ask

What is a resolved promise in JavaScript?

A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object. The static Promise.resolve function returns a Promise that is resolved.

Is it possible to use a resolved promise in synchronous code?

No, but you don't technically have to wait for the promise to resolve immediately, you can call then or catch (or async try/catch) on the promise whenever you want, even multiple times and manipulate it in different ways. I want to know if there is any way at all to use the data from a resolved promise in 'normal' synchronous code

How do I Resolve and reject a promise?

let promise = new Promise (function (resolve, reject) { reject (new Error ('Something is not right!')); }); An important point to note: A Promise executor should call only one resolve or one reject. Once one state is changed (pending => fulfilled or pending => rejected), that's all. Any further calls to resolve or reject will be ignored.

How to return a value from a promise?

The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.


2 Answers

No, it is not possible to do this. This is by design.

The Promise A+ specification is meant to be used as a simple, consistent way to deal with asynchronous operations. One of the constraints is that passing a new callback on to then() on an already resolved promise will always execute on a later tick in the event loop, so things are consistent.

Adding a secondary way to inspect promise results would not have been impossible. It would probably have been quite easy to do so, but there's at least 2 problems with adding this to the specification:

  1. If you're looking to build a specification, you want it to be as simple as possible. The specification itself actually only defines the then() function.
  2. By adding this feature, you're opening the door to hordes of developers getting even more confused about something that's already hard to grok.

Promises and asynchronous operations are hard for people to understand. I see questions here daily about promises and not 'getting' it. If non-async way to access promise results would be added to the default promise, I'd imagine that this would be an even larger amount. It's good to try and enforce 'the right way' of doing things.

However, this decision is not simply made for you. You're not stuck there. You can very easily build your own version of a promise that has this feature, and still works with existing promises. As long as your object has a then() function that behaves according to Promises/A+ you can do with the rest of the object whatever you want.

like image 112
Evert Avatar answered Sep 25 '22 00:09

Evert


Promises are always asynchronous in JS.

If you're confident that promise is going to resolve then you can access it with .then method.

a.then(function(value) {
    console.log("First");
    console.log(value);
    console.log("Last");
    // expected output: 123
});

Variable a will lookalike below if it is console.log

Promise {<resolved>: 123}
    __proto__: Promise
    [[PromiseStatus]]: "resolved"
    [[PromiseValue]]: 123

For error handling, you can error block as mentioned in MDN docs.

like image 41
Varit J Patel Avatar answered Sep 25 '22 00:09

Varit J Patel