{"id":146,"date":"2026-05-05T11:37:54","date_gmt":"2026-05-05T11:37:54","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=146"},"modified":"2026-05-06T08:49:12","modified_gmt":"2026-05-06T08:49:12","slug":"async-await","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=async-await","title":{"rendered":"async \/ await"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Async and Await are modern features in JavaScript that make asynchronous programming easier to read and write. They are built on Promises and help developers write cleaner and more structured code when dealing with tasks that take time, such as API calls or database operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Asynchronous Programming<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Asynchronous programming allows JavaScript to run tasks in the background without stopping the main execution. This is useful for operations like fetching data, loading files, or communicating with servers. It prevents the application from freezing while waiting for a response.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Async Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An async function is a function declared with the async keyword. It always returns a Promise, even if you return a simple value. Inside an async function, you can use the await keyword.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function greet() {<br>  return \"Hello World\";<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">What is Await<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Await is used inside an async function to pause the execution until a Promise is resolved. It allows JavaScript to wait for a task to complete before moving forward.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function getData() {<br>  let result = await fetch(\"https:\/\/api.example.com\/data\");<br>  let data = await result.json();<br>  console.log(data);<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How Async and Await Work Together<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Async marks a function as asynchronous while await pauses the execution until the Promise is completed. Together they make asynchronous code easier to understand compared to traditional Promise chaining.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Benefits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Makes code easier to read and maintain<br>Simplifies handling of asynchronous operations<br>Reduces complexity compared to callbacks<br>Improves error handling using try and catch<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Error Handling with Async Await<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Error handling is done using try and catch blocks inside async functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function fetchData() {<br>  try {<br>    let response = await fetch(\"https:\/\/api.example.com\");<br>    let data = await response.json();<br>    console.log(data);<br>  } catch (error) {<br>    console.log(\"Error occurred\", error);<br>  }<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Real World Uses<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Fetching data from APIs<br>Handling database requests in backend systems<br>Loading dynamic web content<br>Managing file operations in Node.js applications<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Async and Await are essential tools in modern JavaScript development. They make asynchronous code easier to write, understand, and maintain, especially when working with APIs and server-side operations.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced JavaScript > Asynchronous JavaScript > async \/ await<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777980991013\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777980990788\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-146","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>async \/ await - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript async await to write cleaner asynchronous code, handle APIs easily, and improve web development efficiency.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=async-await\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"async \/ await - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript async await to write cleaner asynchronous code, handle APIs easily, and improve web development efficiency.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=async-await\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-06T08:49:12+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=async-await\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=async-await\",\"name\":\"async \\\/ await - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-05T11:37:54+00:00\",\"dateModified\":\"2026-05-06T08:49:12+00:00\",\"description\":\"Learn JavaScript async await to write cleaner asynchronous code, handle APIs easily, and improve web development efficiency.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=async-await#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=async-await\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=async-await#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced JavaScript > Asynchronous JavaScript > async \\\/ await\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"async \/ await - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript async await to write cleaner asynchronous code, handle APIs easily, and improve web development efficiency.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/javascript\/?lesson=async-await","og_locale":"en_US","og_type":"article","og_title":"async \/ await - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript async await to write cleaner asynchronous code, handle APIs easily, and improve web development efficiency.","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=async-await","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-06T08:49:12+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=async-await","url":"https:\/\/gigz.pk\/javascript\/?lesson=async-await","name":"async \/ await - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-05T11:37:54+00:00","dateModified":"2026-05-06T08:49:12+00:00","description":"Learn JavaScript async await to write cleaner asynchronous code, handle APIs easily, and improve web development efficiency.","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=async-await#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=async-await"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=async-await#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Advanced JavaScript > Asynchronous JavaScript > async \/ await"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}