{"id":153210,"date":"2019-01-23T13:20:34","date_gmt":"2019-01-23T13:20:34","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=153210"},"modified":"2019-01-23T13:20:34","modified_gmt":"2019-01-23T13:20:34","slug":"asynchronous-functions-in-javascript","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/","title":{"rendered":"Asynchronous functions in Javascript"},"content":{"rendered":"<p>I this tutorial, I will discuss a brief about the async function in javascript. Recently I was working on a project where I met the requirement of the asynchronous functions in jquery and I am sharing that experience here. Before discussing the asynchronous functions let us see what do we mean by synchronous.<\/p>\n<p><strong>Synchronous Code:\u00a0<\/strong><br \/>\nSynchronous code or synchronous programming means in sequence i.e statements execute in sequence. Each statement is executed one after the other i.e, a statement has to wait before the other one is finished. If you have three lines of code line1, line2, and line3, then line3 cannot execute until line2 has finished executing and line2 cannot execute until line1 has finished.<\/p>\n<p>Example:<\/p>\n<pre>function synchronous_functions() {\r\n   function1();\r\n\u00a0 \u00a0function2();\r\n\u00a0 \u00a0function3();\r\n}<\/pre>\n<p>In the above example, function1 will be executed before function2 and function2 before fucntion3. The execution will start from top to bottom.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Asynchronous Code(Async):<br \/>\n<\/strong>Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence.\u00a0Generally, we use asynchronous code to perform time-consuming operations. Async is the latest addition to the jquery.<br \/>\nAsync\/await is a new syntax that allows us to compose Promises as though they were just normal synchronous functions without callbacks. It&#8217;s a fantastic addition to the Javascript language, added in 2017\u00a0 in Javascript ES7, and can be used to simplify pretty much any existing JS application.<\/p>\n<p>The unique keyword is async before every declaration. Here is the following syntax:<\/p>\n<ul>\n<li>Async function declarations: async function function1() {}<\/li>\n<li>Async function expressions: const fun = async function2 () {};<\/li>\n<li>Async method definitions: let obj = { async function1() {} }<\/li>\n<li>Async arrow functions: const fun = async () =&gt; {};<\/li>\n<\/ul>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"><\/code><\/pre>\n<div><\/div>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"><span class=\"token punctuation\">functionfunction1(ms) {\r\n     return newPromise(ms);\r\n}\r\nasync function asyncAwaitFunction () {\r\n   const fun = await function1();\r\n    console.log(fun);\r\n}\r\n<\/span><\/code><\/pre>\n<p>Example:<\/p>\n<pre class=\"brush:js\">function timer(ms) {\r\n    return new Promise(res =&gt; setTimeout(res, ms));\r\n }\r\n\u00a0var len = 10;|\r\n\u00a0async function load_slider () {\r\n\u00a0 \u00a0 \u00a0for (var i = 0; i &lt; len; i++) {\r\n        console.log(i);\r\n        await timer(5000);\r\n      }\r\n   }<\/pre>\n<p>That is it.!!!<br \/>\n<span style=\"color: #666699\">If you liked\u00a0this post, It would be very grateful if you write your opinions, comments and suggestions to keep the post updated and interesting.<\/span><br \/>\n<strong><span style=\"color: #666699\">Thank you!<\/span><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I this tutorial, I will discuss a brief about the async function in javascript. Recently I was working on a project where I met the requirement of the asynchronous functions in jquery and I am sharing that experience here. Before discussing the asynchronous functions let us see what do we mean by synchronous. Synchronous Code:\u00a0 <a href=\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":88,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[8087,8088,8090,8089],"class_list":["post-153210","post","type-post","status-publish","format-standard","hentry","category-jquery","tag-async-functions","tag-asynchronous-functions","tag-javascript-async","tag-synchronous-fucntions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Asynchronous functions in Javascript - Webkul Blog async functions<\/title>\n<meta name=\"description\" content=\"Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Asynchronous functions in Javascript - Webkul Blog async functions\" \/>\n<meta property=\"og:description\" content=\"Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-23T13:20:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Jahangir Naik\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jahangir Naik\" \/>\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:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\"},\"author\":{\"name\":\"Jahangir Naik\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d3efd6f1d02c2713752e7cba0b626ca6\"},\"headline\":\"Asynchronous functions in Javascript\",\"datePublished\":\"2019-01-23T13:20:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\"},\"wordCount\":302,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"async functions\",\"asynchronous functions\",\"javascript async\",\"synchronous fucntions\"],\"articleSection\":[\"jQuery\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\",\"url\":\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\",\"name\":\"Asynchronous functions in Javascript - Webkul Blog async functions\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2019-01-23T13:20:34+00:00\",\"description\":\"Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Asynchronous functions in Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d3efd6f1d02c2713752e7cba0b626ca6\",\"name\":\"Jahangir Naik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/20021fdcdb324166dd5aef0f183ffb391facf8853dd58f9fcabc0950f4118c01?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/20021fdcdb324166dd5aef0f183ffb391facf8853dd58f9fcabc0950f4118c01?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Jahangir Naik\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/jahangir260\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Asynchronous functions in Javascript - Webkul Blog async functions","description":"Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence.","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:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Asynchronous functions in Javascript - Webkul Blog async functions","og_description":"Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence.","og_url":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2019-01-23T13:20:34+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Jahangir Naik","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Jahangir Naik","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/"},"author":{"name":"Jahangir Naik","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d3efd6f1d02c2713752e7cba0b626ca6"},"headline":"Asynchronous functions in Javascript","datePublished":"2019-01-23T13:20:34+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/"},"wordCount":302,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["async functions","asynchronous functions","javascript async","synchronous fucntions"],"articleSection":["jQuery"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/","url":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/","name":"Asynchronous functions in Javascript - Webkul Blog async functions","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2019-01-23T13:20:34+00:00","description":"Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/asynchronous-functions-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Asynchronous functions in Javascript"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d3efd6f1d02c2713752e7cba0b626ca6","name":"Jahangir Naik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/20021fdcdb324166dd5aef0f183ffb391facf8853dd58f9fcabc0950f4118c01?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/20021fdcdb324166dd5aef0f183ffb391facf8853dd58f9fcabc0950f4118c01?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Jahangir Naik"},"url":"https:\/\/webkul.com\/blog\/author\/jahangir260\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/153210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=153210"}],"version-history":[{"count":17,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/153210\/revisions"}],"predecessor-version":[{"id":160044,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/153210\/revisions\/160044"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=153210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=153210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=153210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}