{"id":8602,"date":"2025-02-23T11:00:39","date_gmt":"2025-02-23T05:30:39","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8602"},"modified":"2025-02-23T11:00:43","modified_gmt":"2025-02-23T05:30:43","slug":"async-await","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/async-await\/","title":{"rendered":"Async\/Await"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The async\/await syntax is a special syntax created to help you work with promise objects. It makes your code cleaner and clearer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When handling Promise, we need to chain the call to the function or variable that returns a\u00a0Promise using\u00a0then\/catch methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Async Keyword<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We use the\u00a0async keyword with a function to represent that the function is an asynchronous function. The async function returns a promise.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">async function name(parameter1, parameter2, \u2026paramaterN) {<br>\/\/ statements<br>}<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"187\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10-1024x187.png\" alt=\"\" class=\"wp-image-8603\" style=\"width:763px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10-1024x187.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10-300x55.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10-768x140.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10-1536x281.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10-2048x374.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.35.10-850x155.png 850w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this snippet the\u00a0async\u00a0keyword is used before the function to represent that the function is asynchronous.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since this function returns a promise, we can use the chaining method\u00a0then<code>()<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38.png\"><img decoding=\"async\" width=\"1024\" height=\"228\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38-1024x228.png\" alt=\"\" class=\"wp-image-8604\" style=\"width:759px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38-1024x228.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38-300x67.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38-768x171.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38-1536x342.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38-850x189.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.37.38.png 2038w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this snippet <code>f()<\/code>\u00a0function is resolved and the\u00a0then<code>()<\/code>\u00a0method gets executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Await Keyword<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The\u00a0await keyword is used inside the\u00a0async\u00a0function to wait for the asynchronous operation. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">let result = await promise<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The use of\u00a0await\u00a0pauses the async function until the promise returns a result (resolve or reject) value. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37.png\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37-1024x288.png\" alt=\"\" class=\"wp-image-8605\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37-1024x288.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37-300x84.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37-768x216.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37-1536x432.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37-2048x576.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.41.37-850x239.png 850w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this snipper a\u00a0Promise\u00a0object is created and it gets resolved after 4000 milliseconds. Here, the\u00a0asyncFunc<code>()<\/code>\u00a0function is written using the\u00a0async\u00a0function. The\u00a0await\u00a0keyword waits for the promise to be complete (resolve or reject).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence,\u00a0hello\u00a0is displayed only after promise value is available to the\u00a0result\u00a0variable. If\u00a0<code>await<\/code>\u00a0is not used,\u00a0hello\u00a0is displayed before\u00a0Promise resolved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Error Handling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While using the\u00a0<code>async<\/code>\u00a0function, we  write the code in a synchronous manner. And you can also use the\u00a0catch<code>()<\/code>\u00a0method to catch the error.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"353\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44-1024x353.png\" alt=\"\" class=\"wp-image-8606\" style=\"width:727px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44-1024x353.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44-300x104.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44-768x265.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44-1536x530.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44-850x293.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-23-at-10.47.44.png 1982w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this snippet try\/catch\u00a0block to handle the errors. If the program runs successfully, it will go to the\u00a0try\u00a0block. And if the program throws an error, it will go to the\u00a0catch\u00a0block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Promise vs Async\/Await Code Comparison<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the async\/await version, the result of the promise is directly assigned to a variable. In the standard promise version, the result of the promise is passed as an argument to the\u00a0.then()\u00a0method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most of them prefer the async\/await version as it looks more straightforward.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"320\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-1024x320.png\" alt=\"\" class=\"wp-image-8607\" style=\"width:724px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-1024x320.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-300x94.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-768x240.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-1536x480.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1-850x266.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/02\/image-1.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use Async\/Await in IIFE and Arrow Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An Immediately Invoked Function Expression (IIFE) is a technique used to execute a function immediately as soon as you define it.Unlike regular functions and variables, IIFEs will be removed from the running process once they are executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Aside from the standard function, we can also make an asynchronous IIFE. This is useful when we need to run the asynchronous function only once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(async function () {<br>try {<br>const response = await fetch(&#8216;https:\/\/jsonplaceholder.typicode.com\/todos\/1&#8217;);<br>const json = await response.json();<br>console.log(json);<br>} catch (error) {<br>console.log(error);<br>}<br>})();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00a0Arrow syntax when creating an asynchronous function<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\">const runProcess = async () =&gt; {<br>try {<br>const response = await fetch(&#8216;https:\/\/jsonplaceholder.typicode.com\/todos\/1&#8217;);<br>const json = await response.json();<br>console.log(json);<br>} catch (error) {<br>console.log(error);<br>}<br>};<\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\">runProcess();<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"benefits\">Benefits of Using async Function<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The code is more readable than using a\u00a0callback or a promise.<\/li>\n\n\n\n<li>Error handling is simpler.<\/li>\n\n\n\n<li>Debugging is easier.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The async\/await syntax is a special syntax created to help you work with promise objects. It makes your code cleaner [&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-8602","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2eK","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8602","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=8602"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8602\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}