{"id":254,"date":"2023-12-27T16:29:30","date_gmt":"2023-12-27T16:29:30","guid":{"rendered":"https:\/\/clvrclvr.com\/content\/?p=254"},"modified":"2023-12-27T16:35:56","modified_gmt":"2023-12-27T16:35:56","slug":"async-and-await","status":"publish","type":"post","link":"https:\/\/clvrclvr.com\/content\/?p=254","title":{"rendered":"async and await"},"content":{"rendered":"\n<p>(This article written with the assistance of GitHub copilot AI.)<\/p>\n\n\n\n<p><strong><em>async <\/em><\/strong>and <strong><em>await <\/em><\/strong>are keywords in JavaScript that allow you to write asynchronous code in a more readable and concise way. They are based on promises, which are objects that represent the outcome of an asynchronous operation.<\/p>\n\n\n\n<p>To use async and await, you need to declare a function with the async keyword, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function demoAsyncAwait() {\n  \/\/ some code\n}\n<\/code><\/pre>\n\n\n\n<p>This means that the function will always return a promise, even if the return value is not a promise itself. For example, this function returns a promise that resolves to 1:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function demoAsyncAwait() {\n  return 1;\n}\n<\/code><\/pre>\n\n\n\n<p>Inside an async function, you can use the await keyword to pause the execution until a promise is settled, and then resume it with the promise result. For example, this function waits for one second and then returns \u201cdone!\u201d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function demoAsyncAwait() {\n  let promise = new Promise((resolve, reject) =&gt; {\n    setTimeout(() =&gt; resolve(\"done!\"), 1000);\n  });\n\n  let result = await promise; \/\/ wait until the promise resolves\n  return result; \/\/ \"done!\"\n}\n<\/code><\/pre>\n\n\n\n<p>You can call an async function like any other function, but you need to use the .then method or another async function to get the result. For example, this code prints \u201cdone!\u201d after one second:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>demoAsyncAwait().then(console.log); \/\/ \"done!\"\n<\/code><\/pre>\n\n\n\n<p>Or, you can use another async function with await:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function main() {\n  let result = await demoAsyncAwait(); \/\/ get the result of the async function\n  console.log(result); \/\/ \"done!\"\n}\n\nmain();\n<\/code><\/pre>\n\n\n\n<p>Using async and await can make your code more readable and easier to follow, especially when you have multiple asynchronous operations that depend on each other. You can also use try\/catch blocks to handle errors in async functions, instead of using .catch methods on promises.<\/p>\n\n\n\n<p>If you want to learn more about async and await, you can check out these resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a is=\"cib-link\" href=\"https:\/\/javascript.info\/async-await\" target=\"_blank\" rel=\"noreferrer noopener\">Async\/await &#8211; The Modern JavaScript Tutorial<\/a><\/li>\n\n\n\n<li><a is=\"cib-link\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/async_function\" target=\"_blank\" rel=\"noreferrer noopener\">async function &#8211; JavaScript | MDN<\/a><\/li>\n\n\n\n<li><a is=\"cib-link\" href=\"https:\/\/www.sitepoint.com\/javascript-async-await\/\" target=\"_blank\" rel=\"noreferrer noopener\">A Beginner\u2019s Guide to JavaScript async\/await, with Examples<\/a><\/li>\n\n\n\n<li><a is=\"cib-link\" href=\"https:\/\/www.freecodecamp.org\/news\/javascript-async-await\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use Async\/Await in JavaScript \u2013 Explained with Code Examples<\/a><\/li>\n\n\n\n<li><a is=\"cib-link\" href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/asynchronous-programming\/\" target=\"_blank\" rel=\"noreferrer noopener\">Asynchronous programming in C# &#8211; C# | Microsoft Learn<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more<\/p>\n\n\n\n<p><a href=\"https:\/\/javascript.info\/async-await\" target=\"_blank\" rel=\"noreferrer noopener\">1 javascript.info<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/async_function\" target=\"_blank\" rel=\"noreferrer noopener\">2 developer.mozilla.org<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.sitepoint.com\/javascript-async-await\/\" target=\"_blank\" rel=\"noreferrer noopener\">3 sitepoint.com<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.freecodecamp.org\/news\/javascript-async-await\/\" target=\"_blank\" rel=\"noreferrer noopener\">4 freecodecamp.org<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/asynchronous-programming\/\" target=\"_blank\" rel=\"noreferrer noopener\">5 learn.microsoft.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(This article written with the assistance of GitHub copilot AI.) async and&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-post"],"_links":{"self":[{"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=254"}],"version-history":[{"count":2,"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions\/256"}],"wp:attachment":[{"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/clvrclvr.com\/content\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}