How to Determine a JavaScript Promise’s Status

Promises have changed the landscape of JavaScript. Many old APIs have been reincarnated to use Promises (XHR to fetch, Battery API), while new APIs trend toward Promises. Developers can use async/await to handle promises, or then/catch/finally with callbacks, but what Promises don’t tell you is their status. Wouldn’t it be great if the Promise.prototype provided […]

The post How to Determine a JavaScript Promise’s Status appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Promises have changed the landscape of JavaScript. Many old APIs have been reincarnated to use Promises (XHR to fetch, Battery API), while new APIs trend toward Promises. Developers can use async/await to handle promises, or then/catch/finally with callbacks, but what Promises don’t tell you is their status. Wouldn’t it be great if the Promise.prototype provided developers a status property to know whether a promise is rejected, resolved, or just done?

My research led me to this gist which I found quite clever. I took some time to modify a bit of code and add comments. The following solution provides helper methods for determining a Promise’s status:

// Uses setTimeout with Promise to create an arbitrary delay time
// In these examples, a 0 millisecond delay is 
// an instantly resolving promise that we can jude status against
async function delay(milliseconds = 0, returnValue) {
  return new Promise(done => setTimeout((() => done(returnValue)), milliseconds));
}

// Promise.race in all of these functions uses delay of 0 to
// instantly resolve.  If the promise is resolved or rejected,
// returning that value will beat the setTimeout in the race

async function isResolved(promise) {
  return await Promise.race([delay(0, false), promise.then(() => true, () => false)]);
}

async function isRejected(promise) {
  return await Promise.race([delay(0, false), promise.then(() => false, () => true)]);
}

async function isFinished(promise) {
  return await Promise.race([delay(0, false), promise.then(() => true, () => true)]);
}

A few examples of usage:

// Testing isResolved
await isResolved(new Promise(resolve => resolve())); // true
await isResolved(new Promise((_, reject) => reject()));  // false

// Testing isRejected
await isRejected(new Promise((_, reject) => reject())); // true

// We done yet?
await isFinished(new Promise(resolve => resolve())); // true
await isFinished(new Promise((_, reject) => reject()));  // true

Developers can always add another await or then to a Promise to execute something but it is interesting to figure out the status of a given Promise. Is there an easier way to know a Promise’s status? Let me know!

The post How to Determine a JavaScript Promise’s Status appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2023-01-03T21:43:58+00:00) How to Determine a JavaScript Promise’s Status. Retrieved from https://www.scien.cx/2023/01/03/how-to-determine-a-javascript-promises-status/

MLA
" » How to Determine a JavaScript Promise’s Status." David Walsh | Sciencx - Tuesday January 3, 2023, https://www.scien.cx/2023/01/03/how-to-determine-a-javascript-promises-status/
HARVARD
David Walsh | Sciencx Tuesday January 3, 2023 » How to Determine a JavaScript Promise’s Status., viewed ,<https://www.scien.cx/2023/01/03/how-to-determine-a-javascript-promises-status/>
VANCOUVER
David Walsh | Sciencx - » How to Determine a JavaScript Promise’s Status. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/03/how-to-determine-a-javascript-promises-status/
CHICAGO
" » How to Determine a JavaScript Promise’s Status." David Walsh | Sciencx - Accessed . https://www.scien.cx/2023/01/03/how-to-determine-a-javascript-promises-status/
IEEE
" » How to Determine a JavaScript Promise’s Status." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2023/01/03/how-to-determine-a-javascript-promises-status/. [Accessed: ]
rf:citation
» How to Determine a JavaScript Promise’s Status | David Walsh | Sciencx | https://www.scien.cx/2023/01/03/how-to-determine-a-javascript-promises-status/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.