{"id":28579,"date":"2016-11-30T00:15:34","date_gmt":"2016-11-29T16:15:34","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=28579"},"modified":"2025-04-04T02:49:13","modified_gmt":"2025-04-03T18:49:13","slug":"synchronous-asynchronous-javascript-part2","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/","title":{"rendered":"Mastering Synchronous &#038; Asynchronous in JavaScript"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript\/\">first part of this post<\/a>, we saw how the <strong>concepts of synchronous and asynchronous<\/strong> are perceived <strong>in JavaScript<\/strong>. In this second part, Mr X appears again to help us understand <strong>how the setTimeout and AJAX <abbr title=\"Application Programming Interface\">APIs<\/abbr> work<\/strong>.<\/p>\n<h2>An odd request<\/h2>\n<p>Let\u2019s rewind back to the story of Mr X and the movie you want to leave for. Say you leave a task for Mr X before the outing, and tell him that he can only begin to work on this task <strong>five hours <em>after<\/em> he got your message<\/strong>.<\/p>\n<p>He isn\u2019t happy about it, remember, he doesn\u2019t take a new message until he is done with the current one, and if he takes yours, he <strong>has to wait for <em>five<\/em> hours<\/strong> to even start on the task. So, to not be wasteful of time, he <strong>brings in a helper<\/strong>, Mr H.<\/p>\n<p>Instead of waiting, he asks Mr H to <strong>leave a new message for the task in the queue<\/strong> after the given hours had passed, and moves on to the next message.<\/p>\n<p>Five hours past; Mr H <strong>updates the queue<\/strong> with a new message. After he\u2019s done processing all the accrued messages prior to Mr H\u2019s, Mr X <strong>carries out your requested task<\/strong>. So, this way, you can leave a request to be <strong>complied upon at a later time<\/strong>, and not wait until it\u2019s fulfilled.<\/p>\n<p>But why does Mr H leave a message in the queue instead of directly contacting Mr X? Because as I mentioned in the first part, the <em>only<\/em> way to contact Mr X is <strong>by leaving a message to him<\/strong> via phone call \u2014 no exceptions.<\/p>\n<h2>1. The <code>setTimeout()<\/code> method<\/h2>\n<p>Suppose you have a set of code that you want to <strong>execute after a certain time<\/strong>. In order to do that, you just <strong>wrap it in a function<\/strong>, and <strong>add it to a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WindowTimers\/setTimeout\" target=\"_blank\" rel=\"noopener\"><code>setTimeout()<\/code><\/a> method<\/strong> along with the delay time. The syntax of <code>setTimeout()<\/code> is as follows:<\/p>\n<pre>\r\nsetTimeout(function, delay-time, arg...)\r\n<\/pre>\n<p>The <code>arg...<\/code> parameter stands for any argument the function takes, and <code>delay-time<\/code> is to be added in milliseconds. Below you can see a simple code example, that  outputs \u201chey\u201d in the console after 3 seconds.<\/p>\n<pre>\r\nsetTimeout( function() { console.log('hey') }, 3000 );\r\n<\/pre>\n<p>Once <code>setTimeout()<\/code> starts running, <strong>instead of blocking the call stack<\/strong> until the indicated  delay time is over, a <strong>timer is triggered<\/strong>, and the call stack is gradually emptied for the next message (similarly to the correspondence between Mr X and Mr H).<\/p>\n<p>When the timer expires, a new message <strong>joins the queue<\/strong>,  and the event loop picks it up when the call stack is free after processing all the messages before it \u2014 thus the code runs asynchronously.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/js-re-timer.jpg\" alt=\"JavaScript call stack with timer\" width=\"800\" height=\"378\"><\/figure>\n<h2>2. AJAX<\/h2>\n<p>AJAX (Asynchronous JavaScript and XML) is a concept that uses the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/XMLHttpRequest\" target=\"_blank\" rel=\"noopener\"><code>XMLHttpRequest<\/code><\/a> (XHR) API to <strong>make server requests<\/strong> and <strong>handle the responses<\/strong>.<\/p>\n<p>When browsers make server requests without using XMLHttpRequest, the <strong>page refreshes<\/strong> and <strong>reloads its UI<\/strong>. When the processing of requests and responses are handled by the XHR API, and <strong>UI remains unaffected<\/strong>.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/xhr.jpg\" alt=\"XMLHttpRequest Web API\" width=\"800\" height=\"378\"><\/figure>\n<p>So, basically the goal is to <strong>make requests without page reloads<\/strong>. Now, where is the \u201casynchronous\u201d in this? Just using XHR code (which we\u2019ll see in a moment) doesn\u2019t mean it\u2019s AJAX, because the XHR API can <strong>work in both synchronous and asynchronous ways<\/strong>.<\/p>\n<p>XHR <strong>by default<\/strong> is set to <strong>work asynchronously<\/strong>; when a function makes a request using XHR, it <strong>returns without waiting for the response<\/strong>.<\/p>\n<p>If XHR is configured to <strong>be synchronous<\/strong>, then the function waits until the <strong>response is received and processed<\/strong> before returning.<\/p>\n<h3>Code Example 1<\/h3>\n<p>This example presents an <strong><code>XMLHttpRequest<\/code> object creation<\/strong>. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/XMLHttpRequest\/open\" target=\"_blank\" rel=\"noopener\"><code>open()<\/code><\/a> method, validates the request URL, and the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/XMLHttpRequest\/send\" target=\"_blank\" rel=\"noopener\"><code>send()<\/code><\/a> method sends the request.<\/p>\n<pre>\r\nvar xhr = new XMLHttpRequest();\r\nxhr.open(\"GET\", url);\r\nxhr.send();\r\n<\/pre>\n<p>Any direct access to the response data after <code>send()<\/code> will be in vain, because <code>send()<\/code> <strong>doesn\u2019t wait<\/strong> until the request is completed. Remember, XMLHTTPRequest is set to work asynchronously by default.<\/p>\n<h3>Code Example 2<\/h3>\n<p>The <code>hello.txt<\/code> file in this example is a simple text file containing the text \u2018hello\u2019. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/XMLHttpRequest\/response\" target=\"_blank\" rel=\"noopener\"><code>response<\/code><\/a> property of XHR is invalid, since it didn\u2019t output the text \u2018hello\u2019.<\/p>\n<pre>\r\nvar xhr = new XMLHttpRequest();\r\nxhr.open(\"GET\", \"hello.txt\");\r\nxhr.send();\r\ndocument.write(xhr.response);\r\n\/\/ empty string\r\n<\/pre>\n<p>XHR implements a micro-routine that <strong>keeps checking for response<\/strong> in every millisecond, and <strong>triggers complimentary events<\/strong> for the different states a request goes through. When the response is loaded, <strong>a load event is triggered by XHR<\/strong>, which can deliver a valid response.<\/p>\n<pre>\r\nvar xhr = new XMLHttpRequest();\r\nxhr.open(\"GET\", \"hello.txt\");\r\nxhr.send();\r\nxhr.onload = function(){ document.write(this.response) }\r\n\/\/ writes 'hello' to the document\r\n<\/pre>\n<p>The response inside the load event <strong>outputs \u2018hello\u2019<\/strong>, the correct text.<\/p>\n<p>Going the asynchronous way is preferred, as it doesn\u2019t block other scripts until the request is completed.<\/p>\n<p>If the response has to be processed synchronously, we pass <code>false<\/code> as the last argument of <code>open<\/code>, which <strong>flags the XHR API<\/strong> saying it <strong>has to be synchronous<\/strong> (by default the last argument of <code>open<\/code> is <code>true<\/code>, which you needn\u2019t explicitly specify).<\/p>\n<pre>\r\nvar xhr = new XMLHttpRequest();\r\nxhr.open(\"GET\", \"hello.txt\", false);\r\nxhr.send();\r\ndocument.write(xhr.response);\r\n\/\/ writes 'hello' to document\r\n<\/pre>\n<h2>Why learn all this?<\/h2>\n<p>Almost all beginners make some mistakes with asynchronous concepts such as <code>setTimeout()<\/code> and AJAX, for example by assuming <code>setTimeout()<\/code> executes code after the delay time, or by processing response directly inside a function making an AJAX request.<\/p>\n<p>If you know how the puzzle fits, you can <strong>avoid such confusion<\/strong>. You know that the delay time in <code>setTimeout()<\/code> does not indicate the time <strong>when the code execution starts<\/strong>, but the time <strong>when the timer expires<\/strong> and a new message is queued, which will only be processed when the call stack is free to do so.<\/p>\n<p class=\"recommended_top\">\n\t\t\t\t\t<strong>Read Also:<\/strong>\u00a0\n\t\t\t\t\t<a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript\/\">Mastering Synchronous & Asynchronous JavaScript \u2013 Part 1<\/a>\n\t\t\t\t<\/p>","protected":false},"excerpt":{"rendered":"<p>In the first part of this post, we saw how the concepts of synchronous and asynchronous are perceived in JavaScript. In this second part, Mr X appears again to help us understand how the setTimeout and AJAX APIs work. An odd request Let\u2019s rewind back to the story of Mr X and the movie you&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[4117,511],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Mastering Synchronous &amp; Asynchronous in JavaScript - Hongkiat<\/title>\n<meta name=\"description\" content=\"In the first part of this post, we saw how the concepts of synchronous and asynchronous are perceived in JavaScript. In this second part, Mr X appears\" \/>\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.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mastering Synchronous &amp; Asynchronous in JavaScript\" \/>\n<meta property=\"og:description\" content=\"In the first part of this post, we saw how the concepts of synchronous and asynchronous are perceived in JavaScript. In this second part, Mr X appears\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-29T16:15:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-03T18:49:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/js-re-timer.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"Mastering Synchronous &#038; Asynchronous in JavaScript\",\"datePublished\":\"2016-11-29T16:15:34+00:00\",\"dateModified\":\"2025-04-03T18:49:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/\"},\"wordCount\":870,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/synchronous-asynchronous-javascript-part2\\\/js-re-timer.jpg\",\"keywords\":[\"Javascripts\",\"Web Developers\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/\",\"name\":\"Mastering Synchronous & Asynchronous in JavaScript - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/synchronous-asynchronous-javascript-part2\\\/js-re-timer.jpg\",\"datePublished\":\"2016-11-29T16:15:34+00:00\",\"dateModified\":\"2025-04-03T18:49:13+00:00\",\"description\":\"In the first part of this post, we saw how the concepts of synchronous and asynchronous are perceived in JavaScript. In this second part, Mr X appears\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/synchronous-asynchronous-javascript-part2\\\/js-re-timer.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/synchronous-asynchronous-javascript-part2\\\/js-re-timer.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/synchronous-asynchronous-javascript-part2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mastering Synchronous &#038; Asynchronous in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Mastering Synchronous & Asynchronous in JavaScript - Hongkiat","description":"In the first part of this post, we saw how the concepts of synchronous and asynchronous are perceived in JavaScript. In this second part, Mr X appears","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.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/","og_locale":"en_US","og_type":"article","og_title":"Mastering Synchronous & Asynchronous in JavaScript","og_description":"In the first part of this post, we saw how the concepts of synchronous and asynchronous are perceived in JavaScript. In this second part, Mr X appears","og_url":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2016-11-29T16:15:34+00:00","article_modified_time":"2025-04-03T18:49:13+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/js-re-timer.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"Mastering Synchronous &#038; Asynchronous in JavaScript","datePublished":"2016-11-29T16:15:34+00:00","dateModified":"2025-04-03T18:49:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/"},"wordCount":870,"commentCount":1,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/js-re-timer.jpg","keywords":["Javascripts","Web Developers"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/","url":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/","name":"Mastering Synchronous & Asynchronous in JavaScript - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/js-re-timer.jpg","datePublished":"2016-11-29T16:15:34+00:00","dateModified":"2025-04-03T18:49:13+00:00","description":"In the first part of this post, we saw how the concepts of synchronous and asynchronous are perceived in JavaScript. In this second part, Mr X appears","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/js-re-timer.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/synchronous-asynchronous-javascript-part2\/js-re-timer.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript-part2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Mastering Synchronous &#038; Asynchronous in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-7qX","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=28579"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28579\/revisions"}],"predecessor-version":[{"id":73734,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28579\/revisions\/73734"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=28579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=28579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=28579"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=28579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}