{"id":575,"date":"2020-08-27T15:18:47","date_gmt":"2020-08-27T15:18:47","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=575"},"modified":"2020-09-11T19:18:34","modified_gmt":"2020-09-11T19:18:34","slug":"what-is-promise-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/what-is-promise-in-javascript\/","title":{"rendered":"What is Promise in JavaScript"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"788\" src=\"https:\/\/www.datatype.co.in\/blog\/wp-content\/uploads\/2020\/08\/promise-in-javascript.jpg\" alt=\"What is Promise in JavaScript\" class=\"wp-image-882\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/08\/promise-in-javascript.jpg 940w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/08\/promise-in-javascript-300x251.jpg 300w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/08\/promise-in-javascript-768x644.jpg 768w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/08\/promise-in-javascript-710x595.jpg 710w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p> A <strong><code>Promise<\/code><\/strong>&nbsp;is an object that represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. <strong>Promise<\/strong> can either be&nbsp;<em>fulfilled<\/em>&nbsp;with a value, or&nbsp;<em>rejected<\/em>&nbsp;with a reason (error).  A promise may be in one of 3 possible states: <strong>fulfilled<\/strong>, <strong>rejected<\/strong>, or <strong>pending<\/strong>. Promises can be attached to callbacks to handle the fulfilled value or the reason for rejection.<\/p>\n\n\n\n<p><strong>The Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let promise = new Promise(function(resolve, reject) {\n  \/\/ Executor\n});<\/code><\/pre>\n\n\n\n<p>The function passed to&nbsp;<code><strong>new Promise<\/strong><\/code>&nbsp;is called the&nbsp;<em>executor<\/em>. When&nbsp;<code><strong>new<\/strong> <strong>Promise<\/strong><\/code>&nbsp;is created, the executor runs automatically and attempts to perform a job . When it is finished with the attempt, it calls&nbsp;<code><strong>resolve<\/strong><\/code>&nbsp;if it was successful or&nbsp;<strong><code>reject<\/code><\/strong>&nbsp;if there was an error.  The arguments&nbsp;<code><strong>resolve<\/strong><\/code>&nbsp;and&nbsp;<code><strong>reject<\/strong><\/code>&nbsp;in the executor are callbacks provided by JavaScript itself. Our code will be only inside the executor. <\/p>\n\n\n\n<p><strong>Consuming Promises:<\/strong><\/p>\n\n\n\n<p>A Promise object serves as a link between the executor and the consuming functions which will receive the result or error. A promise can be consumed using the <code>.then()<\/code> method. The anonymous functions passed as arguments (callbacks) will handle the promise response. You may learn more about callbacks <a rel=\"noreferrer noopener\" aria-label=\"here (opens in a new tab)\" href=\"https:\/\/datatype.co.in\/blog\/what-is-callback-in-javascript\/\" target=\"_blank\">here<\/a>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>promise.then(\n  function(response) { \n      \/\/ handle successful response \n      \/\/ This block of code execute only after receiving response  \n  },\n  function(error) {\n     \/\/ handle error \n  }\n);<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let getPromise = new Promise((resolve, reject) => {\n  \n \/\/ In this example, we use setTimeout() method to simulate async code. \n \/\/ In reality, you will be using consuming API.\n  \n\/\/ Returns \"Success!\" after a sec\nsetTimeout( function() {\n    resolve(\"Success!\"); \n  }, 1000) \n}) \n\ngetPromise.then((response) => {\n  \/\/ response is whatever we passed in the resolve() function above.\n  console.log(\"Promise response: \" + response);\n}, (error){\n  console.log(\"Promise Error: \" + error);\n});<\/code><\/pre>\n\n\n\n<p>Inclusion of <strong><code>async\/await<\/code><\/strong> feature in recent JavaScript edition <strong>ES8 <\/strong>has made working working with async functions and promises much more easier. You can learn more about async\/await here: <a rel=\"noreferrer noopener\" aria-label=\"What is async\/wait in JavaSript (opens in a new tab)\" href=\"https:\/\/datatype.co.in\/blog\/what-is-async-await-in-javascript\/\" target=\"_blank\">What is async\/wait in JavaSript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Promise&nbsp;is an object that represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. Promise can either be&nbsp;fulfilled&nbsp;with a value, or&nbsp;rejected&nbsp;with a reason (error). A&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[195,176,200,217],"class_list":["post-575","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-es6-promise-example","tag-javascript","tag-new-promise","tag-what-is-promise-in-js","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=575"}],"version-history":[{"count":22,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/575\/revisions"}],"predecessor-version":[{"id":883,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/575\/revisions\/883"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}