Saturday, November 15, 2025
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy
T3llam
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
T3llam
No Result
View All Result
Home Services & Software

Tips on how to Decide a JavaScript Promise’s Standing

admin by admin
May 25, 2023
in Services & Software
0
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter

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!

Website performance monitoring
Website performance monitoring

RelatedPosts

The state of strategic portfolio administration

The state of strategic portfolio administration

June 11, 2025
You should utilize PSVR 2 controllers together with your Apple Imaginative and prescient Professional – however you’ll want to purchase a PSVR 2 headset as properly

You should utilize PSVR 2 controllers together with your Apple Imaginative and prescient Professional – however you’ll want to purchase a PSVR 2 headset as properly

June 11, 2025
Consumer Information For Magento 2 Market Limit Vendor Product

Consumer Information For Magento 2 Market Limit Vendor Product

June 11, 2025

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!

Website performance monitoring
Website performance monitoring

Previous Post

AMD’s extraordinary next-gen CPU is so quick, it’ll be utilized in essentially the most highly effective supercomputer ever

Next Post

Tribunal dismisses APM, APP’s petitions in opposition to Sanwo-Olu, others — Nigeria — The Guardian Nigeria Information – Nigeria and World Information

Next Post

Tribunal dismisses APM, APP’s petitions in opposition to Sanwo-Olu, others — Nigeria — The Guardian Nigeria Information – Nigeria and World Information

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • App (3,061)
  • Computing (4,401)
  • Gaming (9,599)
  • Home entertainment (633)
  • IOS (9,534)
  • Mobile (11,881)
  • Services & Software (4,006)
  • Tech (5,315)
  • Uncategorized (4)

Recent Posts

  • WWDC 2025 Rumor Report Card: Which Leaks Had been Proper or Unsuitable?
  • The state of strategic portfolio administration
  • 51 of the Greatest TV Exhibits on Netflix That Will Maintain You Entertained
  • ‘We’re previous the occasion horizon’: Sam Altman thinks superintelligence is inside our grasp and makes 3 daring predictions for the way forward for AI and robotics
  • Snap will launch its AR glasses known as Specs subsequent 12 months, and these can be commercially accessible
  • App
  • Computing
  • Gaming
  • Home entertainment
  • IOS
  • Mobile
  • Services & Software
  • Tech
  • Uncategorized
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. However you may visit Cookie Settings to provide a controlled consent.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analyticsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functionalThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessaryThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-othersThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performanceThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policyThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Save & Accept