{"id":1105,"date":"2023-03-15T19:01:13","date_gmt":"2023-03-16T03:01:13","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=1105"},"modified":"2023-03-16T10:05:46","modified_gmt":"2023-03-16T18:05:46","slug":"promise","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=1105","title":{"rendered":"Promise"},"content":{"rendered":"<p>A promise is a JavaScript object that allows you to make asynchronous (aka async) calls. It produces a value when the async operation completes successfully or produces an error if it doesn&#8217;t complete. A promise may be in one of 3 possible states: fulfilled, rejected, or pending.<\/p>\n<pre>let myPromise = new Promise(function(myResolve, myReject) {\r\n\/\/ \"Producing Code\" (May take some time)\r\n\r\nmyResolve(); \/\/ when successful\r\nmyReject(); \/\/ when error\r\n});\r\n\r\n\/\/ \"Consuming Code\" (Must wait for a fulfilled Promise)\r\nmyPromise.then(\r\nfunction(value) { \/* code if successful *\/ },\r\nfunction(error) { \/* code if some error *\/ }\r\n)<\/pre>\n<p>&nbsp;<\/p>\n<ol>\n<li>A \u201cproducing code\u201d that does something and takes time. For instance, some code that loads the data over a network.<\/li>\n<li>A \u201cconsuming code\u201d that wants the result of the \u201cproducing code\u201d once it\u2019s ready. Many functions may need that result.<\/li>\n<li>A <em>promise<\/em> is a special JavaScript object that links the \u201cproducing code\u201d and the \u201cconsuming code\u201d together. The \u201cproducing code\u201d takes whatever time it needs to produce the promised result, and the \u201cpromise\u201d makes that result available to all of the subscribed code when it\u2019s ready.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>A promise is a JavaScript object that allows you to make asynchronous (aka async) calls. It produces a value when the async operation completes successfully or produces an error if it doesn&#8217;t complete. A promise may be in one of 3 possible states: fulfilled, rejected, or pending. let myPromise = new Promise(function(myResolve, myReject) { \/\/ &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=1105\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Promise<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1105","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/1105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1105"}],"version-history":[{"count":5,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/1105\/revisions"}],"predecessor-version":[{"id":1107,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/1105\/revisions\/1107"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1105"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}