{"id":72200,"date":"2019-10-25T04:21:08","date_gmt":"2019-10-25T11:21:08","guid":{"rendered":""},"modified":"2019-10-25T04:21:08","modified_gmt":"2019-10-25T11:21:08","slug":"writing-asynchronous-tasks-in-modern-javascript","status":"publish","type":"post","link":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/","title":{"rendered":"Writing Asynchronous Tasks In Modern JavaScript"},"content":{"rendered":"<link rel=\"canonical\" href=\"https:\/\/www.smashingmagazine.com\/2019\/10\/asynchronous-tasks-modern-javascript\/\"><title>Writing Asynchronous Tasks In Modern JavaScript<\/title><\/p>\n<article>\n<header>\n<h1>Writing Asynchronous Tasks In Modern JavaScript<\/h1>\n<address>Jeremias Menichelli<\/address>\n<p>                  <time datetime=\"2019-10-25T12:30:59+02:00\">2019-10-25T12:30:59+02:00<\/time><time datetime=\"2019-10-25T11:07:01+00:00\">2019-10-25T11:07:01+00:00<\/time><\/header>\n<p>JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its <strong>synchronous<\/strong> nature, which means the code will run line after line, <em>almost<\/em> as you read it, and secondly that it is <strong>single-threaded<\/strong>, only one command is being executed at any time.<\/p>\n<p>As the language evolved, new artifacts appeared in the scene to allow asynchronous execution; developers tried different approaches while solving more complicated algorithms and data flows, which led to the emergence of new interfaces and patterns around them.<\/p>\n<h3>Synchronous Execution And The Observer Pattern<\/h3>\n<p>As mentioned in the introduction, JavaScript runs the code you write line by line, most of the time. Even in its first years, the language had exceptions to this rule, though they were a few and you might know them already: HTTP Requests, DOM events and time intervals.<\/p>\n<p>If we add an event listener to respond to the click of an element from the user, it doesn\u2019t matter what the language interpreter is running it will <strong>stop<\/strong>, run the code we wrote in the listener callback and then go back to its normal flow.<\/p>\n<p>Same with an interval or a network request, <code>addEventListener<\/code>, <code>setTimeout<\/code>, and <code>XMLHttpRequest<\/code> were the first artifacts to access to asynchronous execution for web developers.<\/p>\n<div data-component=\"FeaturePanel\" data-audience=\"non-subscriber\" data-remove=\"true\"><\/div>\n<p>Though these were exceptions of synchronous execution in JavaScript, it\u2019s crucial to understand that the language is still single-threaded. We can <em>break this synchronicity<\/em> but the interpreter still will run one line of code at a time.<\/p>\n<p>For example, let\u2019s check out a network request.<\/p>\n<pre><code>var request = new XMLHttpRequest();\nrequest.open('GET', '\/\/some.api.at\/server', true);\n\n\/\/ observe for server response\nrequest.onreadystatechange = function() {\n  if (request.readyState === 4 && xhr.status === 200) {\n    console.log(request.responseText);\n  }\n}\n\nrequest.send();\n<\/code><\/pre>\n<p>No matter what is happening, by the time the server comes back, the method assigned to <code>onreadystatechange<\/code> gets called before taking back the program\u2019s code sequence.<\/p>\n<p>Something similar happens when reacting to user interaction.<\/p>\n<pre><code>const button = document.querySelector('button');\n\n\/\/ observe for user interaction\nbutton.addEventListener('click', function(e) {\n  console.log('user click just happened!');\n})\n<\/code><\/pre>\n<p>You might notice that we are hooking up to an external event and passing a callback, telling the code what to do when it takes place. Over a decade ago, \u201cWhat is a callback?\u201d was a pretty much-expected interview question because this pattern was everywhere in most codebases.<\/p>\n<p>In each case mentioned, we are responding to an external event. A certain interval of time reached, a user action or a server response. We weren\u2019t able to create an asynchronous task per se, we always <em>observed<\/em> occurrences happening outside of our reach.<\/p>\n<p>This is why code shaped this way is called the <strong>Observer Pattern<\/strong>, which is better represented by the <code>addEventListener<\/code> interface in this case. Soon event emitters libraries or frameworks exposing this pattern flourished.<\/p>\n<h4>Node.js And Event Emitters<\/h4>\n<p>A good example is Node.js which page describes itself as \u201can asynchronous event-driven JavaScript runtime\u201d, so event emitters and callback were first-class citizens. It even had an <code>EventEmitter<\/code> constructor already implemented.<\/p>\n<pre><code>const EventEmitter = require('events');\nconst emitter = new EventEmitter();\n\n\/\/ respond to events\nemitter.on('gretting', (message) => console.log(message));\n\n\/\/ send events\nemitter.emit('gretting', 'Hi there!');\n<\/code><\/pre>\n<p>This was not only the to-go approach for asynchronous execution but a core pattern and convention of its ecosystem. Node.js opened a new era of writing JavaScript in a different environment \u2014 even outside the web. As a consequence, other asynchronous situations were possible, like creating new directories or writing files.<\/p>\n<pre><code>const { mkdir, writeFile } = require('fs');\n\nconst styles = 'body { background: #ffdead; }';\n\nmkdir('.\/assets\/', (error) => {\n  if (!error) {\n    writeFile('assets\/main.css', styles, 'utf-8', (error) => {\n      if (!error) console.log('stylesheet created');\n    })\n  }\n})\n<\/code><\/pre>\n<p>You might notice that callbacks receive an <code>error<\/code> as a first argument, if a response data is expected, it goes as a second argument. This was called <strong>Error-first Callback Pattern<\/strong>, which became a convention that authors and contributors adopted for their own packages and libraries.<\/p>\n<div><\/div>\n<h3>Promises And The Endless Callback Chain<\/h3>\n<p>As web development faced more complex problems to solve, the need for better asynchronous artifacts appeared. If we look at the last code snippet, we can see a repeated callback chaining which doesn\u2019t scale well as the number tasks increase.<\/p>\n<p>For example, let\u2019s add only two more steps, file reading and styles preprocessing.<\/p>\n<div>\n<pre><code>const { mkdir, writeFile, readFile } = require('fs');\nconst less = require('less')\n\nreadFile('.\/main.less', 'utf-8', (error, data) => {\n  if (error) throw error\n  less.render(data, (lessError, output) => {\n    if (lessError) throw lessError\n    mkdir('.\/assets\/', (dirError) => {\n      if (dirError) throw dirError\n      writeFile('assets\/main.css', output.css, 'utf-8', (writeError) => {\n        if (writeError) throw writeError\n        console.log('stylesheet created');\n      })\n    })\n  })\n})\n<\/code><\/pre>\n<\/div>\n<p>We can see how as the program we are writing gets more complex the code becomes harder to follow for the human eye due to multiple callback chaining and repeated error handling.<\/p>\n<h4>Promises, Wrappers And Chain Patterns<\/h4>\n<p><code>Promises<\/code> didn\u2019t receive much attention when they were first announced as the new addition to the JavaScript language, they aren\u2019t a new concept as other languages had similar implementations decades before. Truth is, they turned out to change a lot the semantics and structure of most of the projects I worked on since its appearance.<\/p>\n<p><code>Promises<\/code> not only introduced a built-in solution for developers to write asynchronous code but also opened a new stage in web development serving as the construction base of later new features of the web spec like <code>fetch<\/code>.<\/p>\n<p>Migrating a method from a callback approach to a promise-based one became more and more usual in projects (such as libraries and browsers), and even Node.js started slowly migrating to them.<\/p>\n<p>Let\u2019s, for example, wrap Node\u2019s <code>readFile<\/code> method:<\/p>\n<pre><code>const { readFile } = require('fs');\n\nconst asyncReadFile = (path, options) => {\n  return new Promise((resolve, reject) => {\n    readFile(path, options, (error, data) => {\n      if (error) reject(error);\n      else resolve(data);\n    })\n  });\n}\n<\/code><\/pre>\n<p>Here we obscure the callback by executing inside a Promise constructor, calling <code>resolve<\/code> when the method result is successful, and <code>reject<\/code> when the error object is defined.<\/p>\n<p>When a method returns a <code>Promise<\/code> object we can follow its successful resolution by passing a function to <code>then<\/code>, its argument is the value which the promise was resolved, in this case, <code>data<\/code>.<\/p>\n<p>If an error was thrown during the method the <code>catch<\/code> function will be called, if present.<\/p>\n<p><strong>Note<\/strong>: <em>If you need to understand more in-depth how Promises work, I recommend Jake Archibald\u2019s \u201c<a href=\"https:\/\/developers.google.com\/web\/fundamentals\/primers\/promises\">JavaScript Promises: An Introduction<\/a>\u201d article which he wrote on Google\u2019s web development blog.<\/em><\/p>\n<p>Now we can use these new methods and avoid callback chains.<\/p>\n<pre><code>asyncRead('.\/main.less', 'utf-8')\n  .then(data => console.log('file content', data))\n  .catch(error => console.error('something went wrong', error))\n<\/code><\/pre>\n<p>Having a native way to create asynchronous tasks and a clear interface to follow up its possible results enabled the industry to move out of the Observer Pattern. Promise-based ones seemed to solve the unreadable and prone-to-error code.<\/p>\n<p><em>As a better syntax highlighting or clearer error messages help while coding, a code that is easier to reason becomes more predictable for the developer reading it, with a better picture of the execution path the easier to catch a possible pitfall.<\/em><\/p>\n<p><code>Promises<\/code> adoption was so global in the community that Node.js rapidly release built-in versions of its I\/O methods to return Promise objects like importing them file operations from <code>fs.promises<\/code>.<\/p>\n<p>It even provided a <code>promisify<\/code> util to wrap any function which followed the Error-first Callback Pattern and transform it into a Promise-based one.<\/p>\n<p><strong>But do Promises help in all cases?<\/strong><\/p>\n<p>Let\u2019s re-imagine our style preprocessing task written with Promises.<\/p>\n<pre><code>const { mkdir, writeFile, readFile } = require('fs').promises;\nconst less = require('less')\n\nreadFile('.\/main.less', 'utf-8')\n  .then(less.render)\n  .then(result =>\n    mkdir('.\/assets')\n      .then(writeFile('assets\/main.css', result.css, 'utf-8'))\n  )\n  .catch(error => console.error(error))\n<\/code><\/pre>\n<p>There is a clear reduction of redundancy in the code, especially around the error handling as we now rely on <code>catch<\/code>, but Promises somehow failed to deliver a clear code indentation that directly relates to the concatenation of actions.<\/p>\n<p>This is actually achieved on the first <code>then<\/code> statement after <code>readFile<\/code> is called. What happens after these lines is the need to create a new scope where we can first make the directory, to later write the result in a file. This causes <em>a break<\/em> into the indentation rhythm, not making it easy to determinate the instructions sequence at first glance.<\/p>\n<p>A way to solve this is to pre-baked a custom method that handles this and allows the correct concatenation of the method, but we would be introducing one more depth of complexity to a code that already seems to have what it needs to achieve the task we want.<\/p>\n<p><strong>Note<\/strong>: <em>Take in count this is an example program, and we are in control around some of the methods and they all follow an industry convention, but that\u2019s not always the case. With more complex concatenations or the introduction of a library with a different shape, our code style can easily break.<\/em><\/p>\n<p>Gladly, the JavaScript community learned again from other language syntaxes and added a notation that helps a lot around these cases where asynchronous tasks concatenation is not as pleasant or straight-forward to read as synchronous code is.<\/p>\n<div><\/div>\n<h3>Async And Await<\/h3>\n<p>A <code>Promise<\/code> is defined as an unresolved value at execution time, and creating an instance of a <code>Promise<\/code> is an <em>explicit<\/em> call of this artifact.<\/p>\n<pre><code>const { mkdir, writeFile, readFile } = require('fs').promises;\nconst less = require('less')\n\nreadFile('.\/main.less', 'utf-8')\n  .then(less.render)\n  .then(result =>\n    mkdir('.\/assets')\n      .then(writeFile('assets\/main.css', result.css, 'utf-8'))\n  )\n  .catch(error => console.error(error))\n<\/code><\/pre>\n<p>Inside an async method, we can use the <code>await<\/code> reserved word to determinate the resolution of a <code>Promise<\/code> before continuing its execution.<\/p>\n<p>Let\u2019s revisit or code snippet using this syntax.<\/p>\n<pre><code>const { mkdir, writeFile, readFile } = require('fs').promises;\nconst less = require('less')\n\nasync function processLess() {\n  const content = await readFile('.\/main.less', 'utf-8')\n  const result = await less.render(content)\n  await mkdir('.\/assets')\n  await writeFile('assets\/main.css', result.css, 'utf-8')\n}\n\nprocessLess()\n<\/code><\/pre>\n<p><strong>Note<\/strong>: <em>Notice that we needed to move all our code to a method because we can\u2019t use<\/em> <code>await<\/code> <em>outside the scope of an async function today.<\/em><\/p>\n<p>Every time an async method finds an <code>await<\/code> statement, it will stop executing until the proceeding value or promise gets resolved.<\/p>\n<p>There\u2019s a clear consequence of using async\/await notation, despite its asynchronous execution, the code looks as if it was <em>synchronous<\/em>, which is something we developers are more used to see and reason around.<\/p>\n<p>What about error handling? For it, we use statements that have been present for a long time in the language, <code>try<\/code> and <code>catch<\/code>.<\/p>\n<div>\n<pre><code>const { mkdir, writeFile, readFile } = require('fs').promises;\nconst less = require('less')\n\nasync function processLess() {\n  const content = await readFile('.\/main.less', 'utf-8')\n  const result = await less.render(content)\n  await mkdir('.\/assets')\n  await writeFile('assets\/main.css', result.css, 'utf-8')\n}\n\ntry {\n  processLess()\n} catch (e) {\n  console.error(e)\n}\n<\/code><\/pre>\n<\/div>\n<p>We rest assured any error thrown in the process will be handled by the code inside the <code>catch<\/code> statement. We have a centric place that takes care of error handling, but now we have a code that is easier to read and follow.<\/p>\n<p>Having consequent actions that returned value doesn\u2019t need to be stored in variables like <code>mkdir<\/code> that don\u2019t break the code rhythm; there\u2019s also no need to create a new scope to access the value of  <code>result<\/code> in a later step.<\/p>\n<p>It\u2019s safe to say Promises were a fundamental artifact introduced in the language, necessary to enable async\/await notation in JavaScript, which you can use on both modern browsers and latest versions of Node.js.<\/p>\n<p><strong>Note<\/strong>: <em>Recently in JSConf, Ryan Dahl, creator and first contributor of Node,<\/em> <a href=\"https:\/\/www.youtube.com\/watch?v=M3BM9TB-8yA\"><em>regretted not sticking to Promises<\/em><\/a> <em>on its early development mostly because the goal of Node was to create event-driven servers and file management which the Observer pattern served better for.<\/em><\/p>\n<h3>Conclusion<\/h3>\n<p>The introduction of Promises into the web development world came to change the way we queue actions in our code and changed how we reason about our code execution and how we author libraries and packages.<\/p>\n<p>But moving away from chains of callback is harder to solve, I think that having to pass a method to <code>then<\/code> didn\u2019t help us to move away from the train of thought after years of being accustomed to the Observer Pattern and approaches adopted by major vendors in the community like Node.js.<\/p>\n<p>As Nolan Lawson says in his <a href=\"https:\/\/pouchdb.com\/2015\/05\/18\/we-have-a-problem-with-promises.html\">excellent article about wrong uses in Promise concatenations<\/a>, <em>old callback habits die hard<\/em>! He later explains how to escape some of these pitfalls.<\/p>\n<p>I believe Promises were needed as a middle step to allow a natural way to generate asynchronous tasks but didn\u2019t help us much to move forward on better code patterns, sometimes you actually need a more adaptable and improved language syntax.<\/p>\n<blockquote>\n<p>\n    <a aria-label=\"Share on Twitter\" href=\"http:\/\/twitter.com\/share?text=As%20we%20try%20to%20solve%20more%20complex%20puzzles%20using%20JavaScript,%20we%20see%20the%20need%20for%20a%20more%20mature%20language%20and%20we%20experiment%20with%20architectures%20and%20patterns%20we%20weren%E2%80%99t%20used%20to%20seeing%20on%20the%20web%20before.%0A&#038;url=https:\/\/smashingmagazine.com%2F2019%2F10%2Fasynchronous-tasks-modern-javascript%2F\"><br \/>\n      As we try to solve more complex puzzles using JavaScript, we see the need for a more mature language and we experiment with architectures and patterns we weren\u2019t used to seeing on the web before.<\/p>\n<p>    <\/a>\n  <\/p>\n<div>\n<div>\n      <span>\u201c<\/span><\/div>\n<\/p><\/div>\n<\/blockquote>\n<p>We still don\u2019t know how the ECMAScript spec will look in years as we are always extending the JavaScript governance outside the web and try to solve more complicated puzzles.<\/p>\n<p>It\u2019s hard to say now what <em>exactly<\/em> we will need from the language for some of these puzzles to turn into simpler programs, but I\u2019m happy with how the web and JavaScript itself are moving things, trying to adapt to challenges and new environments. I feel right now JavaScript is a more <em>asynchronous friendly place<\/em> than when I started writing code in a browser over a decade ago.<\/p>\n<h4>Further Reading<\/h4>\n<ul>\n<li>\u201c<a href=\"https:\/\/developers.google.com\/web\/fundamentals\/primers\/promises\">JavaScript Promises: An Introduction<\/a>,\u201d <em>Jake Archibald<\/em><\/li>\n<li>\u201c<a href=\"https:\/\/github.com\/petkaantonov\/bluebird\/wiki\/Promise-anti-patterns#the-deferred-anti-pattern\">Promise Anti-Patterns<\/a>\u201d, <em>a Bluebird library documentation<\/em><\/li>\n<li>\u201c<a href=\"https:\/\/pouchdb.com\/2015\/05\/18\/we-have-a-problem-with-promises.html\">We Have A Problem With Promises<\/a>,\u201d <em>Nolan Lawson<\/em><\/li>\n<\/ul>\n<div>\n  <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.taterboy.com\/blog\/wp-content\/uploads\/2019\/10\/logo-red-19.png?w=900\" alt=\"Smashing Editorial\"><span>(dm, il)<\/span>\n<\/div>\n<\/article>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing Asynchronous Tasks In Modern JavaScript Writing Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00 JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that&#8230;<a class=\"moretag\" href=\"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/\"> Read the full article&#8230;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[15],"tags":[],"class_list":["post-72200","post","type-post","status-publish","format-standard","hentry","category-general-info"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Guest Contribution\"\/>\n\t<meta name=\"keywords\" content=\"general info\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Design for Immersive Technologies | User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies\" \/>\n\t\t<meta property=\"og:description\" content=\"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-10-25T11:21:08+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2019-10-25T11:21:08+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#article\",\"name\":\"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies\",\"headline\":\"Writing Asynchronous Tasks In Modern JavaScript\",\"author\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"http:\\\/\\\/www.taterboy.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/logo-red-19.png\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#articleImage\"},\"datePublished\":\"2019-10-25T04:21:08-07:00\",\"dateModified\":\"2019-10-25T04:21:08-07:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#webpage\"},\"articleSection\":\"General Info\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.taterboy.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/general-info\\\/#listItem\",\"name\":\"General Info\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/general-info\\\/#listItem\",\"position\":2,\"name\":\"General Info\",\"item\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/general-info\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#listItem\",\"name\":\"Writing Asynchronous Tasks In Modern JavaScript\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#listItem\",\"position\":3,\"name\":\"Writing Asynchronous Tasks In Modern JavaScript\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/general-info\\\/#listItem\",\"name\":\"General Info\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#organization\",\"name\":\"Design for Immersive Technologies\",\"description\":\"User Experience for Games, Virtual Reality (VR) and Augmented\\\/Mixed Reality (AR\\\/MR)\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/\",\"name\":\"Guest Contribution\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d2aad73eb0f48c67b141e9fc978a0e498969791ed623346e361d02d19775b0bb?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Guest Contribution\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#webpage\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/\",\"name\":\"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies\",\"description\":\"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/10\\\/writing-asynchronous-tasks-in-modern-javascript\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\"},\"datePublished\":\"2019-10-25T04:21:08-07:00\",\"dateModified\":\"2019-10-25T04:21:08-07:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/\",\"name\":\"Design for Immersive Technologies\",\"description\":\"User Experience for Games, Virtual Reality (VR) and Augmented\\\/Mixed Reality (AR\\\/MR)\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies","description":"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is","canonical_url":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/","robots":"max-image-preview:large","keywords":"general info","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#article","name":"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies","headline":"Writing Asynchronous Tasks In Modern JavaScript","author":{"@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author"},"publisher":{"@id":"https:\/\/www.taterboy.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"http:\/\/www.taterboy.com\/blog\/wp-content\/uploads\/2019\/10\/logo-red-19.png","@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#articleImage"},"datePublished":"2019-10-25T04:21:08-07:00","dateModified":"2019-10-25T04:21:08-07:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#webpage"},"isPartOf":{"@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#webpage"},"articleSection":"General Info"},{"@type":"BreadcrumbList","@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.taterboy.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/category\/general-info\/#listItem","name":"General Info"}},{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/category\/general-info\/#listItem","position":2,"name":"General Info","item":"https:\/\/www.taterboy.com\/blog\/category\/general-info\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#listItem","name":"Writing Asynchronous Tasks In Modern JavaScript"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#listItem","position":3,"name":"Writing Asynchronous Tasks In Modern JavaScript","previousItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/category\/general-info\/#listItem","name":"General Info"}}]},{"@type":"Organization","@id":"https:\/\/www.taterboy.com\/blog\/#organization","name":"Design for Immersive Technologies","description":"User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)","url":"https:\/\/www.taterboy.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author","url":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/","name":"Guest Contribution","image":{"@type":"ImageObject","@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d2aad73eb0f48c67b141e9fc978a0e498969791ed623346e361d02d19775b0bb?s=96&d=mm&r=g","width":96,"height":96,"caption":"Guest Contribution"}},{"@type":"WebPage","@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#webpage","url":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/","name":"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies","description":"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.taterboy.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/#breadcrumblist"},"author":{"@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author"},"creator":{"@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author"},"datePublished":"2019-10-25T04:21:08-07:00","dateModified":"2019-10-25T04:21:08-07:00"},{"@type":"WebSite","@id":"https:\/\/www.taterboy.com\/blog\/#website","url":"https:\/\/www.taterboy.com\/blog\/","name":"Design for Immersive Technologies","description":"User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.taterboy.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Design for Immersive Technologies | User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)","og:type":"article","og:title":"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies","og:description":"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is","og:url":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/","article:published_time":"2019-10-25T11:21:08+00:00","article:modified_time":"2019-10-25T11:21:08+00:00","twitter:card":"summary","twitter:title":"Writing Asynchronous Tasks In Modern JavaScript | Design for Immersive Technologies","twitter:description":"Writing Asynchronous Tasks In Modern JavaScriptWriting Asynchronous Tasks In Modern JavaScript Jeremias Menichelli 2019-10-25T12:30:59+02:002019-10-25T11:07:01+00:00JavaScript has two main characteristics as a programming language, both important to understand how our code will work. First is its synchronous nature, which means the code will run line after line, almost as you read it, and secondly that it is"},"aioseo_meta_data":{"post_id":"72200","title":null,"description":null,"keywords":null,"keyphrases":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_custom_image_width":null,"og_custom_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"created":"2021-02-07 17:05:24","updated":"2026-09-02 09:02:25","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"twitter_image_url":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.taterboy.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.taterboy.com\/blog\/category\/general-info\/\" title=\"General Info\">General Info<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tWriting Asynchronous Tasks In Modern JavaScript\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.taterboy.com\/blog"},{"label":"General Info","link":"https:\/\/www.taterboy.com\/blog\/category\/general-info\/"},{"label":"Writing Asynchronous Tasks In Modern JavaScript","link":"https:\/\/www.taterboy.com\/blog\/2019\/10\/writing-asynchronous-tasks-in-modern-javascript\/"}],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8wzr5-iMw","jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/posts\/72200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/comments?post=72200"}],"version-history":[{"count":0,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/posts\/72200\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/media?parent=72200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/categories?post=72200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/tags?post=72200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}