What is a JavaScript Promise and how does it work?
A JavaScript Promise is an object representing the eventual completion or failure of an asynchronous operation. It has three states: pending (initial state), fulfilled (operation succeeded), and rejected (operation failed). Promises allow you to handle asynchronous code more elegantly by chaining .then() for success and .catch() for errors. (Source)
What is an unhandled promise rejection and why is it dangerous?
An unhandled promise rejection occurs when a promise fails (is rejected) and no code handles the error. This can cause runtime errors that bubble up and potentially crash your entire application, both in frontend JavaScript and backend Node.js environments. Always handle promise rejections to prevent unexpected application failures. (Source)
How can I correctly handle promise rejections in JavaScript?
You can handle promise rejections using the .catch() method or by wrapping your asynchronous code in a try-catch block when using async/await. This ensures errors are caught and managed, preventing unhandled promise rejection errors. (Source)
Product Information
What is Hygraph and what is its primary purpose?
Hygraph is a GraphQL-native Headless CMS designed to help teams build better digital experiences. Its primary purpose is to provide a flexible, scalable, and developer-friendly platform for managing content across multiple channels, removing traditional CMS pain points, and enabling rapid innovation. (Source)
Who is the target audience for Hygraph?
Hygraph is designed for developers, IT decision-makers, content creators, project managers, agencies, and technology partners. It is especially valuable for modern software companies, enterprises seeking to modernize, and brands aiming to scale content operations across geographies and market segments. (Source)
Features & Capabilities
What are the key features and capabilities of Hygraph?
Hygraph offers Smart Edge Cache for fast content delivery, Content Federation to integrate data from multiple sources, Rich Text SuperPowers for advanced formatting, Custom Roles for granular access control, Project Backups, and developer-friendly APIs. It supports seamless integration with eCommerce, localization, and other systems, and is SOC 2 Type 2, ISO 27001, and GDPR compliant. (Source)
Does Hygraph provide APIs for developers?
Yes, Hygraph provides GraphQL Content API, GraphQL Management API, Public API, and supports both REST and GraphQL APIs for efficient content querying, management, and integration with external systems. (Source)
What integrations are available with Hygraph?
Hygraph integrates with digital asset management tools (Aprimo, AWS S3, Bynder, Cloudinary, Mux, Scaleflex Filerobot), hosting platforms (Netlify, Vercel), eCommerce solutions (BigCommerce, commercetools, Shopify), localization tools (Lokalise, Crowdin, EasyTranslate, Smartling), personalization (Ninetailed), AI (AltText.ai), and more. (Source)
Use Cases & Benefits
What problems does Hygraph solve for businesses?
Hygraph solves operational inefficiencies (removing developer bottlenecks, improving workflows), financial challenges (reducing costs, accelerating speed-to-market), and technical issues (simplifying schema evolution, cache management, and integration with multiple endpoints). It enables editorial autonomy and supports global content operations. (Source)
What business impact can customers expect from using Hygraph?
Customers can expect up to 3X faster time-to-market (Komax), 15% higher customer engagement (Samsung), 20% increase in website monetization (AutoWeb), 7X higher content velocity, and 125% growth in traffic. Hygraph supports content operations across 40+ global markets and enables rapid scaling. (Source)
Which industries are represented in Hygraph's case studies?
Can you share specific customer success stories using Hygraph?
Yes. Komax achieved 3X faster time-to-market and managed 20,000+ product variations across 40+ markets. Samsung saw a 15% increase in customer engagement. Dr. Oetker improved global consistency and workflows. Sennheiser increased e-commerce conversions by 136.7% in 4 months. Stobag grew online revenue share from 15% to 70%. (Source)
Technical Requirements & Documentation
How easy is it to implement Hygraph and get started?
Hygraph is recognized as the #1 easiest to implement headless CMS. For example, Top Villas launched a new project in just 2 months. Non-technical users find the UI intuitive and logical, and developers can start immediately with a free API playground. Structured onboarding and training resources are available. (Source)
What training and technical support does Hygraph offer?
Hygraph provides onboarding support (introduction call, account provisioning, business/technical/content kickoffs), comprehensive documentation, webinars, live streams, hands-on guidance, and 24/7 technical support via chat, email, phone, and Slack. Enterprise customers receive a dedicated Customer Success Manager and SLA-backed support. (Source)
What technical documentation is available for Hygraph?
Hygraph offers comprehensive documentation, API references, guides for content workflows, webhooks, and interactive API playgrounds. These resources support both technical and non-technical users. (Source)
Security & Compliance
What security and compliance certifications does Hygraph have?
Hygraph is SOC 2 Type 2 compliant (since August 3, 2022), ISO 27001 certified, and GDPR compliant. It offers granular permissions, audit logs, encryption, SSO integrations, automatic backups, and supports enterprise-grade compliance with dedicated hosting and custom SLAs. (Source)
Customer Proof & Testimonials
Who are some of Hygraph's customers?
Hygraph is trusted by leading brands including Sennheiser, HolidayCheck, Ancestry, JDE, Dr. Oetker, Ashley Furniture, Lindex, Hairhouse, Komax, Shure, Stobag, Burrow, G2I, Epic Games, Bandai Namco, Gamescom, Leo Vegas, Codecentric, Voi, and Clayton Homes. (Source)
What feedback have customers given about Hygraph's ease of use?
Customers consistently praise Hygraph for its intuitive user interface, logical workflows, and accessibility for non-technical users. The editor UI is described as clear and easy to use, and the platform enables both technical and non-technical teams to collaborate efficiently. (Source)
How to handle an Unhandled Promise Rejection in JavaScript
The states of JavaScript promises can be pending, fulfilled, or rejected. Let's take a look at what you should do when there is an "unhandled promise rejection".
Last updated by AagamĀ
onĀ Aug 20, 2024
Originally written by Joel
The states of JavaScript promises can be pending, fulfilled, or rejected. Let's look at what you should do when there is an "unhandled promise rejection".
A Promise is a special JavaScript object representing an asynchronous operation's eventual fulfillment or failure. It is similar to making a promise in real life, where you promise to do something in the future. A promise always has two outcomes: you either do it by keeping your Promise or you do not.
JavaScript promises are complex but straightforward to grasp. A promise object has two properties: state and result. The state can be pending, fulfilled, or rejected; the result can be undefined or the value of the fulfilled or rejected state.
Pending: This is the initial state of the promise when it is being processed. This is the initial state before the promise succeeds or fails and has a result of undefined.
Fulfilled: This is the completed and successful state of a promise. It returns the resolve value as a result.
Rejected: Like the resolved or fulfilled states, the rejected state indicates a failed promise. If a promise ends up in the rejected state, the error value is returned as a result.
In clear terms, the promise state is initially pending with a result of undefined; when the Promise's condition is true, the state is fulfilled and has a result with the value of resolve(value); otherwise, when the condition fails, it has an error value of reject (error).
For example, the code block below is a Promise that checks a condition. If the condition is true it resolves otherwise, it rejects.
const myPromise =newPromise((resolve, reject)=>{
let cms ="Hygraph";
if(cms ==="Hygraph"){
resolve("Success: The promise has successfully resolved!");
}else{
reject("Failure: The promise has failed!");
}
});
Editor's Note
A promise that is either resolved or rejected is called settled.
The getUsersPromise function returns a promise that simulates fetching user data. The promise uses setTimeout to delay the resolution by 1 second. When resolved, it returns the DUMMY_USERS_DATA array. There is also a commented-out section that, if uncommented, would simulate an error by rejecting the promise with the message "something went wrong..." after 1 second.
We can simply use a .then() handler method to consume a promise.
functiongetUserList(){
returngetUsersPromise().then((data)=> data);
}
We can also use async await syntax as well to consume a promise.
Many things can get a promise rejected, such as some run time error or a network failure.
When an error arises within a promise, it gets rejected and calls the reject() function. Unhandled promise rejections imply that when a promise is rejected, it is not handled. In other words, it is unhandled because nothing deals with the rejection.
For example:
functiongetUsersPromise(){
constDUMMY_USERS_DATA=[{id:1,name:"John Doe"}];
returnnewPromise((resolve, reject)=>{
// Fake Error
setTimeout(()=>{
reject("something went wrong...");
},1000);
});
}
asyncfunctiongetUserList(){
returnawaitgetUsersPromise();
}
// OR
// function getUserList() {
// return getUsersPromise().then((data) => data);
// }
getUserList();
Above is a promise that fails, for simplicity, we have faked and called the reject() function after a delay of 1s. Okay but, what is the big deal about rejected promises and what exactly is an unhandled promise rejection?
The promise getUsersPromise in the above code is called by the getUserList function, the main issue here is that if the promise rejects which it will, no code is handling the promise rejection in the getUserList function. This would generate an āUnhandled Promise Rejectionā error. This error can bubble up to your application root and can even stop your entire application.
Also, this is not just applicable to frontend Javascript code, but also to backend Node.js-based codebases.
For example, check this demo video:
In this demo, we have two different routes in a Node.js-based Express application.
GET /hello
GET /user
If you notice we hit the /hello endpoint that says Hello World. Once we hit the /user with an unhandled promise, our entire backed app crashes! Our rejected promise had nothing to do with the /hello endpoint, but it still went down as the app itself crashed. This can be really dangerous in production.
To handle promise rejections specifically, we can use the .catch() handler method. A more popular option is to wrap the promise call in a try-catch block.
// Using .catch() handler
functiongetUserList(){
returngetUsersPromise()
.then((data)=> data)
.catch((err)=>console.error(err));
}
// Using a try-catch wrapper
asyncfunctiongetUserList(){
try{
returnawaitgetUsersPromise();
}catch(err){
console.error(err);
}
}
Thatās as simple as it could be.
Let us also explore an example using the Fetch API. The Fetch API is a built-in JavaScript method for retrieving resources and interacting with your backend server or an API endpoint. It returns a promise, meaning you can use the .then() and .catch() handler methods.
Letās retrieve data by sending a GraphQL request with Fetch API to the Cocktails Hygraph content repository which holds some cocktails and some information about each cocktail:
In the above example, rejection is not handled. In a Fetch API request, the Promise can be rejected due to network issues, permission issues, and other run time reasons. We can attach a catch handler to it to catch any unforeseen issues, as shown below:
In this article, you have learned what are promises, how to use them. We also saw what exactly is an unhandled promise rejection and how dangerous it can be. Finally, we went through how to handle promises the correct way and saw examples around it.
Feel free to customize and handle rejections of promises however you'd like, but always make sure to handle rejections of promises when working with promises since your web application may run into run-time errors.
Blog Authors
Aagam Vadecha
Joel Olawanle
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.