This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies. COOKIE POLICY COOKIE POLICY
The simplest explanation of a ‘Promise’ in JavaScript can be referenced to a promise in real life. When creating a new promise, the promise constructor takes 2 arguments ‘resolve’ and ‘reject.’ Resolve means fulfilling the promises set forth. In other words, completing what you said you’re going to do. Reject means promise is not fulfilled, in other words not completing what was set forth. The code below shows how easy it is to create a new promise.
When the promise is executed we will get back a result. If the promise is successful it will return a ‘resolve,’ then we can utilize the Promise.then() function.
In the code below, we can pass in an argument, ‘fromResolve,’ to the Promise.then() function. This argument will show us the status of the resolve.
If the promise is not fulfilled we can then catch the result in a function called Promise.catch().
The code above showed that we chain the Promise.catch() after the Promise.then() and pass in an argument of ‘fromReject.’
Nested Promises
Let me add more complexity, in the code below I’ve created 3 different promises. We can then nest the promises together based on different rules and get the desired result.
Let say you don’t want to nest the promises and you don’t want to wait for one to finish and the second to start. We then can use the Promise.all() method to accomplish this objective and once they are all done it returns a result.
Promise Race
Another use case would be if you’re trying to pull the same data from three different servers. You could use the Promise.race() method to accomplish this. Below is the code example.
Conclusion
Now, you know what a promise is and how to create one in JavaScript. Give it a try! I hope this gave you a better and more simple understanding of Promises in JavaScript.
Reid Braswell Joins UDig as Vice President, Software Engineering
UDig is proud to welcome Reid Braswell as our new Vice President of Software Engineering. With over 13 years of experience in technology consulting, Reid brings deep expertise in digital transformation, modern software engineering, and client-focused solutions. His leadership and passion for solving complex challenges make him an exceptional addition to the UDig team. Reid’s […]
Energy 2025 – Expansion of Fossil Fuels or Carbon Reduction?
Now that the election is behind us, we have an opportunity to anticipate the possible effects on the energy industry under this new administration. What strategies will be impacted? What will remain the same? What opportunities can we take advantage of in 2025? This blog is meant to dig into these questions and provide some […]
Embracing Digital Accessibility: A Moral and Business Imperative In today’s digital landscape, accessibility has become crucial for businesses and organizations. With increasing awareness and legal requirements, ensuring that digital products are accessible to all users, including those with disabilities, is not just a compliance issue but a moral imperative. At UDig, we champion ADA compliance […]
Unlocking Business Potential: The Power of Custom Application Development
Like any savvy business leader, you’re likely always on the lookout for tools to give your company a competitive edge. And in doing so, you’ve undoubtedly considered investing in custom application development. But the question is, how do you ensure that such a major investment in a custom web application development provides a strong return on […]
Mastering Legacy Application Modernization: Strategies for Success
The ironic truth of the business world is that change is the only constant. But this means that failing to keep pace with the competition and its technologies will only end with you falling behind. That’s where legacy application modernization enters the fold. When you modernize legacy applications, your team gains access to new features […]
In this episode of CTO Confessions, Rob Phillips, the Vice President of Software Engineering at UDig, digs into his journey from a passionate technologist in his youth to a seasoned leader in the tech industry. He shares valuable lessons on transitioning to senior leadership, the importance of understanding and articulating company problems, and the art of empowering teams for high performance.