{"id":8482,"date":"2025-01-20T22:46:30","date_gmt":"2025-01-20T17:16:30","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8482"},"modified":"2025-01-20T22:46:40","modified_gmt":"2025-01-20T17:16:40","slug":"promise-and-methods","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/promise-and-methods\/","title":{"rendered":"Promise and Methods"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A promise is used to perform asynchronous tasks in JavaScript because JavaScript is a single-threaded programming language.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Three possible states in Promise<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pending<\/strong>: The task is still in progress.<\/li>\n\n\n\n<li><strong>Fulfilled<\/strong>: The task completed successfully.<\/li>\n\n\n\n<li><strong>Rejected<\/strong>: The task failed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example of promise<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"334\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25-1024x334.png\" alt=\"\" class=\"wp-image-8484\" style=\"width:651px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25-1024x334.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25-300x98.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25-768x251.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25-1536x501.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25-850x277.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-21.45.25.png 1930w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Methods of Promise<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Promise.all()<\/li>\n\n\n\n<li>Promise.any()<\/li>\n\n\n\n<li>Promise.race()<\/li>\n\n\n\n<li>Promise.finally()<\/li>\n\n\n\n<li>Promise.allsettled()<\/li>\n\n\n\n<li>Promise.catch()<\/li>\n\n\n\n<li>Promise.reject()<\/li>\n\n\n\n<li>Promise.resolve()<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.catch()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This method runs once all promises are completed, whether they are resolved or rejected. It is used to handle errors if any occur.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.reject()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This method is used to reject the promise if it fails or encounters an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.resolve()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This method is used to indicate that if the promise is successful, it will be in the resolved state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.all()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a function used to handle multiple asynchronous operation concurrently. It take an array of promises and return a single promise that resolves when all promises in array have resolves or reject if any promises in the array rejects.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20.png\"><img decoding=\"async\" width=\"1024\" height=\"478\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20-1024x478.png\" alt=\"\" class=\"wp-image-8487\" style=\"width:505px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20-1024x478.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20-300x140.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20-768x358.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20-1536x717.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20-643x300.png 643w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.37.20.png 1972w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.any()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It an array of promises and returns the first fulfilled promise. If no promise is fulfilled, it rejects with an<code> error<\/code>, which wraps multiple errors into a single error.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21.png\"><img decoding=\"async\" width=\"1024\" height=\"464\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21-1024x464.png\" alt=\"\" class=\"wp-image-8488\" style=\"width:644px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21-1024x464.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21-300x136.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21-768x348.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21-1536x696.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21-662x300.png 662w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.39.21.png 2036w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.finally()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a promise is completed, either resolved or rejected, the specified callback function is executed. This helps avoid duplicating code in both the promise&#8217;s then() and catch() handlers.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"518\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42-1024x518.png\" alt=\"\" class=\"wp-image-8489\" style=\"width:505px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42-1024x518.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42-300x152.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42-768x389.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42-1536x778.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42-2048x1037.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.42.42-593x300.png 593w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.race()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This method returns a promise that resolves or rejects as soon as one of the promises in an iterable (such as an array) fulfills or rejects, with the corresponding value or reason.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"519\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54-1024x519.png\" alt=\"\" class=\"wp-image-8490\" style=\"width:553px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54-1024x519.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54-300x152.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54-768x390.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54-1536x779.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54-2048x1039.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.43.54-592x300.png 592w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Promise.allsettled()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This method takes an array of promises, waits for all of them to complete (whether resolved or rejected), and returns a promise.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"433\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06-1024x433.png\" alt=\"\" class=\"wp-image-8491\" style=\"width:533px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06-1024x433.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06-300x127.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06-768x325.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06-1536x649.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06-2048x865.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-20-at-22.46.06-710x300.png 710w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Need for Promise:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If a function is waiting for a resource or performing a network request, this takes time. Meanwhile, the entire execution freezes. This is where asynchronous programming comes into play with the help of features like callbacks, promises, and async\/await. These features allow us to perform time-consuming actions without affecting the main execution thread.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A promise is used to perform asynchronous tasks in JavaScript because JavaScript is a single-threaded programming language. Three possible states [&hellip;]<\/p>\n","protected":false},"author":517,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-8482","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2cO","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/517"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8482"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8482\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}