{"id":888,"date":"2021-02-14T21:49:31","date_gmt":"2021-02-14T21:49:31","guid":{"rendered":"http:\/\/www.ishygddt.xyz\/~blog\/?p=888"},"modified":"2022-03-08T19:00:54","modified_gmt":"2022-03-08T19:00:54","slug":"basic-js-async-cookbook","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2021\/02\/basic-js-async-cookbook","title":{"rendered":"Basic JS Async Cookbook"},"content":{"rendered":"<pre><code class=\"language-javascript\" data-line=\"\">function asleep(delay) {\n\treturn new Promise(resolve =&gt; setTimeout(resolve, delay));\n}\n\n\/\/ Usage:\nawait asleep(1000);<\/code><\/pre>\n<pre><code class=\"language-javascript\" data-line=\"\">function aalert(message) {\n\treturn new Promise( resolve =&gt; resolve(alert(message)) );\n}\nfunction aconfirm(message) {\n\treturn new Promise( resolve =&gt; resolve(confirm(message)) );\n}\nfunction aprompt(message, deflt) {\n\treturn new Promise( resolve =&gt; resolve(prompt(message, deflt)) );\n}\n\n\/\/ Usage:\nawait aalert(&quot;The task will begin as soon as you press OK.&quot;) ; do_task();\naalert(&quot;The task will begin as soon as you press OK.&quot;).then(do_task);\n\nif( await aconfirm(&quot;Do you want to do the task?&quot;) ) do_task();\naconfirm(&quot;Do you want to do the task?&quot;).then( response =&gt; response ? do_task() : undefined );\n\nusername = await aprompt(&quot;Enter your username.&quot;);\n<\/code><\/pre>\n<pre><code class=\"language-javascript\" data-line=\"\">\/\/ aXMLHttpRequest: a &quot;handmade&quot; version of fetch, for educational purposes\nfunction aXMLHttpRequest(method, location, responseType=&quot;&quot;, body=undefined) {\n\treturn new Promise( (resolve, reject) =&gt; {\n\t\tconst xhr = new XMLHttpRequest();\n\t\txhr.open(method, location, true);\n\t\txhr.responseType = responseType;\n\t\txhr.onload = resolve;\n\t\txhr.onerror = reject;\n\t\txhr.send(body);\n\t\treturn xhr;\n\t});\n}\n<\/code><\/pre>\n<p>I'll happily take suggestions\/requests for more recipes here in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>function asleep(delay) { return new Promise(resolve =&gt; setTimeout(resolve, delay)); } \/\/ Usage: await asleep(1000); function aalert(message) { return new Promise( resolve =&gt; resolve(alert(message)) ); } function aconfirm(message) { return new Promise( resolve =&gt; resolve(confirm(message)) ); } function aprompt(message, deflt) { return new Promise( resolve =&gt; resolve(prompt(message, deflt)) ); } \/\/ Usage: await aalert(&quot;The task will &hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[75,40,144],"class_list":["post-888","post","type-post","status-publish","format-standard","hentry","category-drafts","tag-asynchronous","tag-javascript","tag-programming"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/comments?post=888"}],"version-history":[{"count":16,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/888\/revisions"}],"predecessor-version":[{"id":1707,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/888\/revisions\/1707"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=888"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}