{"id":4346,"date":"2022-04-12T10:32:14","date_gmt":"2022-04-12T10:32:14","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4346"},"modified":"2022-04-12T10:32:15","modified_gmt":"2022-04-12T10:32:15","slug":"javascript-promises","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/04\/12\/javascript-promises\/","title":{"rendered":"Javascript &#8211; Promises"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Promises are a way of handling asynchronous events in javascript. Prior to promises, the only true asynchronous feature was in <em>XMLHttpRequest<\/em> where ajax calls were made. <\/p>\n\n\n\n<p>A Promise is an object whose constructor takes in an anonymous function. The anonymous function has two arguments &#8211; resolve and reject. When the code within the anonymous functions finishes, it can either call resolve or reject . Resolve means it was successful and Reject means that some problem or error occurred.<\/p>\n\n\n\n<p>The constructor defines the actual processing code and the calls to resolve and reject. The handling of the resolve and reject callback functions are done using the <em>then<\/em> and <em>catch <\/em>method of the Promise object. The <em>then <\/em>method will be called if <em>resolve<\/em> was executed. The catch method will be called if <em>reject<\/em> was executed.<\/p>\n\n\n\n<p>The sample source is given below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n\t&lt;head&gt;\n\t\t\n\t\t&lt;title&gt;Javascript Promises&lt;\/title&gt;\n\t\n\t  &lt;script&gt;\n\n\t\n\t\tfunction doIt() {\n\t\t        var promise = new Promise(function(resolve, reject) {\n\t\t\t  var x = Math.floor(Math.random()*10);\t\n\t\t\t  if (x % 2 == 0)\n\t\t\t\t  resolve(x);\n\t\t\t  else\n\t\t\t\t  reject(x);\n\t\t  });\n\n\t\t  promise.then(function(x) {\n\t\t  \talert(x + &quot; was even&quot;);\n\t\t  }).catch(function(x) {\n\t\t\talert( x + &quot; was not even&quot;);\t\t  \n\t\t  });\n\n\t\t}\n\t  &lt;\/script&gt;\n\t&lt;\/head&gt;\n\n\t&lt;body&gt;\n\n\t\t&lt;button type=button onclick=&quot;doIt(); return false;&quot;&gt;Click Me&lt;\/button&gt;\n\t&lt;\/body&gt;\n&lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<p>Depending on whether the random number is even or odd, the relevant alert will show. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Promises are a way of handling asynchronous events in javascript. Prior to promises, the only true asynchronous feature was in XMLHttpRequest where ajax calls <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/04\/12\/javascript-promises\/\" title=\"Javascript &#8211; Promises\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-4346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4346"}],"version-history":[{"count":1,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4346\/revisions"}],"predecessor-version":[{"id":4347,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4346\/revisions\/4347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}