{"id":22912,"date":"2018-10-08T12:15:43","date_gmt":"2018-10-08T09:15:43","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=22912"},"modified":"2018-10-08T09:24:05","modified_gmt":"2018-10-08T06:24:05","slug":"understanding-ordering-js-async-methods","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/","title":{"rendered":"Understanding ordering with JavaScript async methods"},"content":{"rendered":"<p>So I challenged someone in a code review to prove that there code that made use of async functions wouldn&#8217;t be susceptible to a race condition. To that end I came up with a very trivial code example to demonstrate the issue, worth trying to write down the output and line orderings before you read on.<\/p>\n<pre class=\"brush:js\">let list;\r\n\r\nasync function clearList () {\r\n    list = []; \/\/ A\r\n}\r\n\r\nasync function processList (processList) {\r\n   await clearList(); \/\/ B\r\n   list = list.concat(processList); \/\/ C\r\n}\r\n\r\nprocessList([1,2,3]); \/\/ D\r\nprocessList([4,5,6]) \/\/ E\r\n   .then(() =&gt; {\r\n      console.dir(list); \/\/ F\r\n   })<\/pre>\n<p>So the two questions here are what is the output of this code and what order do the lines of code get executed in. Now the point of the code was that it would show that the output is<br \/>\n<code>[1,2,3,4,5,6]<\/code> because of the race condition; but the actual ordering of the execution of the lines of code I had wrong.<\/p>\n<p>My assumption was that you would not enter the async method directly and instead be queued to be performed on a later clock tick. This gave me an execution flow of <code>D,E,B,B,A,A,C,C,F<\/code> which I was happy with until my coworker Millan Kuchtiak pointed out I was entirely incorrectly.<\/p>\n<p>It turns out that at least in Chrome and Safari that code doesn&#8217;t get transferred queue until you reach the first await call. So actually when you run the code through with a debugger the flow is <code>D,B,A,E,B,A,C,C,F<\/code>. This make sense from a performance point of view, some async methods might never need the change of context, so just in time async.<\/p>\n<p>To summarise\u00a0<b>an async method is synchronous up until the first await<\/b><\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Web Code Geeks with permission by Gerard Davison, partner at our <a href=\"\/\/www.webcodegeeks.com\/join-us\/wcg\/\" target=\"_blank\" rel=\"noopener\">WCG program<\/a>. See the original article here: <a href=\"http:\/\/kingsfleet.blogspot.com\/2018\/10\/understanding-ordering-with-javascript.html\" target=\"_blank\" rel=\"noopener\">Understanding ordering with JavaScript async methods<\/a><\/p>\n<p>Opinions expressed by Web Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>So I challenged someone in a code review to prove that there code that made use of async functions wouldn&#8217;t be susceptible to a race condition. To that end I came up with a very trivial code example to demonstrate the issue, worth trying to write down the output and line orderings before you read &hellip;<\/p>\n","protected":false},"author":55,"featured_media":920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-22912","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding ordering with JavaScript async methods - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Interested to learn about async method? Check our article explaining how to use JavaScript async methods in understanding ordering.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding ordering with JavaScript async methods - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Interested to learn about async method? Check our article explaining how to use JavaScript async methods in understanding ordering.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-08T09:15:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Gerard Davison\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gerard Davison\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/\"},\"author\":{\"name\":\"Gerard Davison\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/abbec5e9d481da352f31b7aeedcc5acd\"},\"headline\":\"Understanding ordering with JavaScript async methods\",\"datePublished\":\"2018-10-08T09:15:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/\"},\"wordCount\":279,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/\",\"name\":\"Understanding ordering with JavaScript async methods - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"datePublished\":\"2018-10-08T09:15:43+00:00\",\"description\":\"Interested to learn about async method? Check our article explaining how to use JavaScript async methods in understanding ordering.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Understanding ordering with JavaScript async methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/abbec5e9d481da352f31b7aeedcc5acd\",\"name\":\"Gerard Davison\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3328b0eb2ba0cbf63dde5eca8855fae1bb66ddf6a8ea42967f984151c1c7a9f0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3328b0eb2ba0cbf63dde5eca8855fae1bb66ddf6a8ea42967f984151c1c7a9f0?s=96&d=mm&r=g\",\"caption\":\"Gerard Davison\"},\"sameAs\":[\"http:\/\/kingsfleet.blogspot.com\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/gerard-davison\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Understanding ordering with JavaScript async methods - Web Code Geeks - 2026","description":"Interested to learn about async method? Check our article explaining how to use JavaScript async methods in understanding ordering.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/","og_locale":"en_US","og_type":"article","og_title":"Understanding ordering with JavaScript async methods - Web Code Geeks - 2026","og_description":"Interested to learn about async method? Check our article explaining how to use JavaScript async methods in understanding ordering.","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2018-10-08T09:15:43+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","type":"image\/jpeg"}],"author":"Gerard Davison","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Gerard Davison","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/"},"author":{"name":"Gerard Davison","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/abbec5e9d481da352f31b7aeedcc5acd"},"headline":"Understanding ordering with JavaScript async methods","datePublished":"2018-10-08T09:15:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/"},"wordCount":279,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/","name":"Understanding ordering with JavaScript async methods - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","datePublished":"2018-10-08T09:15:43+00:00","description":"Interested to learn about async method? Check our article explaining how to use JavaScript async methods in understanding ordering.","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/understanding-ordering-js-async-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Understanding ordering with JavaScript async methods"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/abbec5e9d481da352f31b7aeedcc5acd","name":"Gerard Davison","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3328b0eb2ba0cbf63dde5eca8855fae1bb66ddf6a8ea42967f984151c1c7a9f0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3328b0eb2ba0cbf63dde5eca8855fae1bb66ddf6a8ea42967f984151c1c7a9f0?s=96&d=mm&r=g","caption":"Gerard Davison"},"sameAs":["http:\/\/kingsfleet.blogspot.com\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/gerard-davison\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/22912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/55"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=22912"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/22912\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/920"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=22912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=22912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=22912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}