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

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?

I know that I can use #then() to schedule work to be performed asynchronously after the Promise changes state. I am NOT asking how to do this.

This question is specifically about synchronous interrogation of a Promise’s state. How can I achieve this?

28 Answers
28

Leave a Comment