{"id":144,"date":"2026-05-05T11:24:39","date_gmt":"2026-05-05T11:24:39","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=144"},"modified":"2026-05-05T11:24:41","modified_gmt":"2026-05-05T11:24:41","slug":"promises","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=promises","title":{"rendered":"Promises"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Promises in JavaScript are used to handle asynchronous operations in a cleaner and more organized way. They help developers manage tasks like fetching data from a server, reading files, or performing background operations without blocking the main program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is a Promise<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Promise is an object that represents the result of an asynchronous operation. It can be in one of three states<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pending means the operation is still in progress<br>Fulfilled means the operation completed successfully<br>Rejected means the operation failed<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why Use Promises<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Promises make code easier to read and maintain compared to traditional callback functions<br>They help avoid callback nesting issues known as callback hell<br>They improve error handling using a structured approach<br>They allow chaining multiple asynchronous operations<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Creating a Promise<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Promise is created using the Promise constructor. It takes a function with two parameters resolve and reject<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let myPromise = new Promise(function(resolve, reject) {<br>  let success = true;<br><br>  if (success) {<br>    resolve(\"Operation successful\");<br>  } else {<br>    reject(\"Operation failed\");<br>  }<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Handling Promises<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Promises are handled using then catch and finally methods<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then is used when the promise is fulfilled<br>catch is used when the promise is rejected<br>finally runs regardless of the result<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myPromise<br>  .then(function(result) {<br>    console.log(result);<br>  })<br>  .catch(function(error) {<br>    console.log(error);<br>  })<br>  .finally(function() {<br>    console.log(\"Task completed\");<br>  });<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Chaining Promises<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Promises can be chained to perform multiple asynchronous operations step by step<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function stepOne() {<br>  return new Promise(resolve =&gt; {<br>    resolve(\"Step One Completed\");<br>  });<br>}<br><br>function stepTwo(data) {<br>  return new Promise(resolve =&gt; {<br>    resolve(data + \" and Step Two Completed\");<br>  });<br>}<br><br>stepOne()<br>  .then(stepTwo)<br>  .then(function(result) {<br>    console.log(result);<br>  });<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Real World Use Case<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Promises are commonly used when working with APIs. For example fetching data from a server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"https:\/\/api.example.com\/data\")<br>  .then(response =&gt; response.json())<br>  .then(data =&gt; console.log(data))<br>  .catch(error =&gt; console.log(error));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Advantages of Promises<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Improves code readability and structure<br>Provides better error handling<br>Supports chaining of asynchronous tasks<br>Widely supported in modern JavaScript<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Promises are an essential part of modern JavaScript development. They simplify asynchronous programming and make code more efficient and manageable for real world applications.<\/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 > Promises<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777980233519\"><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-1777980233301\"><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-144","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>Promises - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript promises to handle asynchronous tasks easily with clear examples and improve your coding skills effectively\" \/>\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=promises\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Promises - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript promises to handle asynchronous tasks easily with clear examples and improve your coding skills effectively\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=promises\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-05T11:24:41+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=promises\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=promises\",\"name\":\"Promises - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-05T11:24:39+00:00\",\"dateModified\":\"2026-05-05T11:24:41+00:00\",\"description\":\"Learn JavaScript promises to handle asynchronous tasks easily with clear examples and improve your coding skills effectively\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=promises#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=promises\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=promises#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced JavaScript > Asynchronous JavaScript > Promises\"}]},{\"@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":"Promises - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript promises to handle asynchronous tasks easily with clear examples and improve your coding skills effectively","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=promises","og_locale":"en_US","og_type":"article","og_title":"Promises - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript promises to handle asynchronous tasks easily with clear examples and improve your coding skills effectively","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=promises","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-05T11:24:41+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=promises","url":"https:\/\/gigz.pk\/javascript\/?lesson=promises","name":"Promises - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-05T11:24:39+00:00","dateModified":"2026-05-05T11:24:41+00:00","description":"Learn JavaScript promises to handle asynchronous tasks easily with clear examples and improve your coding skills effectively","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=promises#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=promises"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=promises#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Advanced JavaScript > Asynchronous JavaScript > Promises"}]},{"@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\/144","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=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}