How can I synchronously determine a JavaScript Promise's state?

# Ron Waldon (6 years ago)

I have lodged the following question on StackOverflow:

I have also lodged it as a proposal on Specifiction:

I have a pure JavaScript Promise (built-in implementation or poly-fill):

var promise = new Promise(function (resolve, reject) { /* ... */ });

From the [specification](, a Promise can be one of:

  • 'settled' and 'resolved'
  • 'settled' and 'rejected'
  • 'pending'

I have a use case where I wish to interrogate the Promise synchronously and determine:

  • is the Promise settled?
  • if so, is the Promise resolved?

It appears there is no API for synchronous interrogation of a Promise's state. Can we please make this part of a future specification?

# Domenic Denicola (6 years ago)

I will repeat to you what I said on Specifiction:

To get a standard API for this, you'll need to convince the JavaScript standard committee, as well as the browser vendors, that your use case is widespread and important enough to be worth the standardization and implementation burden, and that it cannot be achieved in any other possible way.

So ... go!

Looking forward to your use cases, preferably with examples showing code in popular libraries or apps that would benefit to illustrate how wide-spread those use cases are.

# C. Scott Ananian (6 years ago)

Why not just use a subclass of Promise? I don't see why that has to be part of the base class.

class ObservablePromise extends Promise {}
ObservablePromise.prototype.isSettled = function() { return
!!this.isSettled; };
ObservablePromise.prototype.then = function(res, rej) {
  return super.then(
    function(x) { this.isSettled = true; return res(x); },
    function(e) { this.isSettled = true; return rej(x); }
// etc
# Ron Waldon (6 years ago)

legacy use case

I am maintaining an existing API that includes asynchronous functions (mix of callbacks and Promises) and synchronous functions. After some asynchronous initialisation, the internal state settles and it is perfectly safe to use the synchronous functions as expected.

So, I'd like to emit warnings when these synchronous functions are called prior to a Promise being "settled". That way, downstream developers will know that they should be waiting for the Promise to settle before using such functions.

This actually isn't too different to the XHR / Fetch APIs conceptually. We get the ball rolling with an asynchronous API call, but there are deterministic blocks within which we can synchronously interrogate progress, etc.

activity indicator use case

I use a Promise to represent a network transaction. I wish to alter the visual state of my web app to reflect the state of this network transaction. I can, for example, show an indeterminate progress bar whilst the Promise is not "settled".

If I am using requestAnimationFrame, or a framework like React, then the state would be synchronously mapped to the DOM / canvas during each execution of my render function.

I can track the state of the Promise using additional variables (as others have suggested), but those state values already exist somewhere private per the functioning of a Promise. I'd be duplicating work that the JavaScript engine is already performing internally, at the risk of introducing errors in my code.

third-party popular libraries

The following libraries implement some form of Promise and all expose such synchronous inspection capabilities: