Guarantees have modified the panorama of JavaScript. Many elderly APIs have been reincarnated to make use of Guarantees (XHR to fetch, Battery API), whereas new APIs pattern towards Guarantees. Builders can use async/await to deal with guarantees, or then/catch/lastly with callbacks, however what Guarantees do not let you know is their standing. Would not it’s nice if the Promise.prototype offered builders a standing property to know whether or not a promise is rejected, resolved, or simply executed?
My analysis led me to this gist which I discovered fairly intelligent. I took a while to switch a little bit of code and add feedback. The next answer gives helper strategies for figuring out a Promise’s standing:
// Makes use of setTimeout with Promise to create an arbitrary delay time
// In these examples, a 0 millisecond delay is
// an immediately resolving promise that we are able to jude standing towards
async perform delay(milliseconds = 0, returnValue) {
return new Promise(executed => setTimeout((() => executed(returnValue)), milliseconds));
}
// Promise.race in all of those capabilities makes use of delay of 0 to
// immediately resolve. If the promise is resolved or rejected,
// returning that worth will beat the setTimeout within the race
async perform isResolved(promise) {
return await Promise.race([delay(0, false), promise.then(() => true, () => false)]);
}
async perform isRejected(promise) {
return await Promise.race([delay(0, false), promise.then(() => false, () => true)]);
}
async perform isFinished(promise) {
return await Promise.race([delay(0, false), promise.then(() => true, () => true)]);
}
A couple of examples of utilization:
// 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 executed but? await isFinished(new Promise(resolve => resolve())); // true await isFinished(new Promise((_, reject) => reject())); // true
Builders can all the time add one other await or then to a Promise to execute one thing however it’s fascinating to determine the standing of a given Promise. Is there a neater strategy to know a Promise’s standing? Let me know!
Guarantees have modified the panorama of JavaScript. Many elderly APIs have been reincarnated to make use of Guarantees (XHR to fetch, Battery API), whereas new APIs pattern towards Guarantees. Builders can use async/await to deal with guarantees, or then/catch/lastly with callbacks, however what Guarantees do not let you know is their standing. Would not it’s nice if the Promise.prototype offered builders a standing property to know whether or not a promise is rejected, resolved, or simply executed?
My analysis led me to this gist which I discovered fairly intelligent. I took a while to switch a little bit of code and add feedback. The next answer gives helper strategies for figuring out a Promise’s standing:
// Makes use of setTimeout with Promise to create an arbitrary delay time
// In these examples, a 0 millisecond delay is
// an immediately resolving promise that we are able to jude standing towards
async perform delay(milliseconds = 0, returnValue) {
return new Promise(executed => setTimeout((() => executed(returnValue)), milliseconds));
}
// Promise.race in all of those capabilities makes use of delay of 0 to
// immediately resolve. If the promise is resolved or rejected,
// returning that worth will beat the setTimeout within the race
async perform isResolved(promise) {
return await Promise.race([delay(0, false), promise.then(() => true, () => false)]);
}
async perform isRejected(promise) {
return await Promise.race([delay(0, false), promise.then(() => false, () => true)]);
}
async perform isFinished(promise) {
return await Promise.race([delay(0, false), promise.then(() => true, () => true)]);
}
A couple of examples of utilization:
// 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 executed but? await isFinished(new Promise(resolve => resolve())); // true await isFinished(new Promise((_, reject) => reject())); // true
Builders can all the time add one other await or then to a Promise to execute one thing however it’s fascinating to determine the standing of a given Promise. Is there a neater strategy to know a Promise’s standing? Let me know!


