{"id":56521,"date":"2023-01-24T21:15:57","date_gmt":"2023-01-24T15:45:57","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=56521"},"modified":"2024-01-02T17:41:49","modified_gmt":"2024-01-02T12:11:49","slug":"top-level-await-without-async-function-in-nodejs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/","title":{"rendered":"Top level await() without async() function in NodeJs"},"content":{"rendered":"<p><img decoding=\"async\" alt=\"Blog image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/0uJsNKFahmenV5AhB.jpg\" \/><\/p>\n<p>Here\u2019s how async-await function typically works:<\/p>\n<pre><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">myDummyBackendCall<\/span>() {\r\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">Promise<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">resolve<\/span>) =&gt;<\/span> {\r\n<span class=\"hljs-built_in\">setTimeout<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n<span class=\"hljs-title.function\">resolve<\/span>({\r\n<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Shivang\"<\/span>,\r\n<span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-string\">\"asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku\"<\/span>,\r\n});\r\n});\r\n});\r\n}\r\n\r\n<span class=\"hljs-comment\">\/\/ Used an async wrapper function to make an await call<\/span>\r\n(<span class=\"hljs-keyword\">async<\/span> () =&gt; <mark class=\"anf ang jk\">{<\/mark>\r\n<span class=\"hljs-keyword\">let<\/span> data = <span class=\"hljs-keyword\">await<\/span> <span class=\"hljs-title.function\">myDummyBackendCall<\/span>();\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(data);\r\n})();<\/pre>\n<p>If you are a JS developer, you would know that without\u00a0<code class=\"em agj agk agl aga b\">async<\/code>\u00a0function,\u00a0<code class=\"em agj agk agl aga b\">await<\/code>\u00a0call will run into an error. And rightly so. The error will look something like this:<\/p>\n<p><img decoding=\"async\" alt=\"Blog image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1_mAoF4ks8F3legp5VcSM3A.png\" \/><\/p>\n<h3 id=\"43c5\" class=\"agn age abu bu wr ago agp agq pp agr ags agt pu pv agu pw pz qa agv qb qe qf agw qg qj agx bx\">Let\u2019s now talk about a different scenario<\/h3>\n<p>Here\u2019s another example of my index.js file inside my NodeJs project wherein I haven\u2019t enclosed my\u00a0<code class=\"em agj agk agl aga b\">await<\/code>\u00a0call inside an\u00a0<code class=\"em agj agk agl aga b\">async<\/code>function (Node version that I am using is 19.3.0):<\/p>\n<pre><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">myDummyBackendCall<\/span>() {\r\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">Promise<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">resolve<\/span>) =&gt;<\/span> {\r\n<span class=\"hljs-built_in\">setTimeout<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n<span class=\"hljs-title.function\">resolve<\/span>({\r\n<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Shivang\"<\/span>,\r\n<span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-string\">\"asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku\"<\/span>,\r\n});\r\n});\r\n});\r\n}\r\n\r\n<span class=\"hljs-comment\">\/\/Note that there is no async wrapper function<\/span>\r\n\r\n<span class=\"hljs-keyword\">let<\/span> data = <span class=\"hljs-keyword\">await<\/span> <span class=\"hljs-title.function\">myDummyBackendCall<\/span>();\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(data);<\/pre>\n<pre>\/\/ Output of node index.js\r\n{\r\nname: <span class=\"hljs-string\">'Shivang'<\/span>,\r\n<span class=\"hljs-built_in\">id<\/span>: <span class=\"hljs-string\">'asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku'<\/span>\r\n}<\/pre>\n<p>You must be wondering how this is done and trust me, it\u2019s fairly simple. All you have to do is to add a simple configuration, \u201ctype\u201d with \u201cmodule\u201d as its value in the package.json file. Here\u2019s an example:<\/p>\n<pre>{\r\n<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"src\"<\/span>,\r\n<span class=\"hljs-string\">\"version\"<\/span>: <span class=\"hljs-string\">\"1.0.0\"<\/span>,\r\n<span class=\"hljs-string\">\"description\"<\/span>: <span class=\"hljs-string\">\"\"<\/span>,\r\n<span class=\"hljs-string\">\"main\"<\/span>: <span class=\"hljs-string\">\"index.js\"<\/span>,\r\n<span class=\"hljs-string\">\"type\"<\/span>:<span class=\"hljs-string\">\"module\"<\/span>, <span class=\"hljs-comment\">\/\/ I'm here!<\/span>\r\n<span class=\"hljs-string\">\"scripts\"<\/span>: {\r\n<span class=\"hljs-string\">\"test\"<\/span>: <span class=\"hljs-string\">\"echo \\\\\"<\/span><span class=\"hljs-title.class\">Error<\/span>: no test specified\\\\<span class=\"hljs-string\">\" &amp;&amp; exit 1\"<\/span>\r\n},\r\n<span class=\"hljs-string\">\"author\"<\/span>: <span class=\"hljs-string\">\"\"<\/span>,\r\n<span class=\"hljs-string\">\"license\"<\/span>: <span class=\"hljs-string\">\"ISC\"<\/span>\r\n}<\/pre>\n<h3 id=\"86e7\" class=\"agn age abu bu wr ago agp agq pp agr ags agt pu pv agu pw pz qa agv qb qe qf agw qg qj agx bx\">Behind the scenes<\/h3>\n<p id=\"4f46\" class=\"pw-post-body-paragraph abs abt abu ql b abv agy vz abx aby agz wc aca acb aha acd ace acf ahb ach aci acj ahc acl acm acn nd bx\" data-selectable-paragraph=\"\">Within a\u00a0<code class=\"em agj agk agl aga b\">package.json<\/code>\u00a0,\u00a0<code class=\"em agj agk agl aga b\">\u201ctype\u201d<\/code>\u00a0value of\u00a0<code class=\"em agj agk agl aga b\">\u201cmodule\u201d<\/code>\u00a0tells NodeJs to interpret the files with\u00a0<code class=\"em agj agk agl aga b\">.js<\/code>\u00a0extension as using ES module syntax, e.g.:\u00a0<code class=\"em agj agk agl aga b\">import<\/code>\u00a0statement or\u00a0<code class=\"em agj agk agl aga b\">import()<\/code>\u00a0expressions.<\/p>\n<p id=\"0f87\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\"><code class=\"em agj agk agl aga b\">\u201ctype\u201d<\/code>\u00a0<strong class=\"ql mx\">defines how NodeJs runtime should resolve specifiers or load modules.<\/strong><\/p>\n<p id=\"d62b\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\">There are two possible values for\u00a0<code class=\"em agj agk agl aga b\">\u201ctype\u201d<\/code>\u00a0config:<\/p>\n<ol class=\"\">\n<li id=\"19cf\" class=\"ahd ahe abu ql b abv afq aby afr acb ahf acf ahg acj ahh acn ahi ahj ahk dz bx\" data-selectable-paragraph=\"\">module<\/li>\n<li id=\"31c1\" class=\"ahd ahe abu ql b abv ahl aby ahm acb ahn acf aho acj ahp acn ahi ahj ahk dz bx\" data-selectable-paragraph=\"\">commonjs\u00a0<strong class=\"ql mx\"><em class=\"ahq\">(default)<\/em><\/strong><\/li>\n<\/ol>\n<p id=\"c93b\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\">There are ways to mix things up without adding\u00a0<code class=\"em agj agk agl aga b\">type<\/code>\u00a0to the\u00a0<code class=\"em agj agk agl aga b\">package.json<\/code>\u00a0file. Since we know that\u00a0<code class=\"em agj agk agl aga b\">commonjs<\/code>\u00a0is the default value applied to your NodeJs project, every file with\u00a0<code class=\"em agj agk agl aga b\">.js<\/code>\u00a0extension will be treated as CommonJS. But if you still want to load a file(s) as an ES Module, simply give the file\u00a0<code class=\"em agj agk agl aga b\">.mjs<\/code>\u00a0extension.<\/p>\n<p id=\"84f2\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\">Here\u2019s the sample code (without\u00a0<code class=\"em agj agk agl aga b\">type<\/code>\u00a0configuration):<\/p>\n<pre>{\r\n<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"src\"<\/span>,\r\n<span class=\"hljs-string\">\"version\"<\/span>: <span class=\"hljs-string\">\"1.0.0\"<\/span>,\r\n<span class=\"hljs-string\">\"description\"<\/span>: <span class=\"hljs-string\">\"\"<\/span>,\r\n<span class=\"hljs-string\">\"main\"<\/span>: <span class=\"hljs-string\">\"index.js\"<\/span>,\r\n<span class=\"hljs-string\">\"scripts\"<\/span>: {\r\n<span class=\"hljs-string\">\"test\"<\/span>: <span class=\"hljs-string\">\"echo \\\\\"<\/span><span class=\"hljs-title.class\">Error<\/span>: no test specified\\\\<span class=\"hljs-string\">\" &amp;&amp; exit 1\"<\/span>\r\n},\r\n<span class=\"hljs-string\">\"author\"<\/span>: <span class=\"hljs-string\">\"\"<\/span>,\r\n<span class=\"hljs-string\">\"license\"<\/span>: <span class=\"hljs-string\">\"ISC\"<\/span>\r\n}<\/pre>\n<p data-selectable-paragraph=\"\">Here\u2019s the screenshot of the\u00a0<code class=\"em agj agk agl aga b\">.mjs<\/code>\u00a0file:<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" alt=\"Blog image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1jfy63LIDdrtYTCnMXKhbjw.png\" \/><\/p>\n<p data-selectable-paragraph=\"\">Here\u2019s the code inside\u00a0<code class=\"em agj agk agl aga b\">index.mjs<\/code>\u00a0(exactly same code as above):<\/p>\n<pre><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">myDummyBackendCall<\/span>() {\r\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">Promise<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">resolve<\/span>) =&gt;<\/span> {\r\n<span class=\"hljs-built_in\">setTimeout<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n<span class=\"hljs-title.function\">resolve<\/span>({\r\n<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Shivang\"<\/span>,\r\n<span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-string\">\"asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku\"<\/span>,\r\n});\r\n});\r\n});\r\n}\r\n\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-keyword\">await<\/span> <span class=\"hljs-title.function\">myDummyBackendCall<\/span>());<\/pre>\n<p data-selectable-paragraph=\"\">Here\u2019s the result:<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" alt=\"Blog image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1y51bT_SiP3in8l3vvl-t9w.png\" \/><\/p>\n<h3 id=\"907d\" class=\"agn age abu bu wr ago agp agq pp agr ags agt pu pv agu pw pz qa agv qb qe qf agw qg qj agx bx\">Here are some points straight from the\u00a0<a class=\"ax aht\" href=\"https:\/\/nodejs.org\/api\/packages.html#packagejson-and-file-extensions\" target=\"_blank\" rel=\"noopener ugc nofollow\">official NodeJs documentation<\/a><\/h3>\n<ol class=\"\">\n<li id=\"dfd0\" class=\"ahd ahe abu ql b abv agy aby agz acb ahu acf ahv acj ahw acn ahi ahj ahk dz bx\" data-selectable-paragraph=\"\">Files ending with\u00a0<code class=\"em agj agk agl aga b\">.mjs<\/code>\u00a0are always loaded as ES modules regardless of the nearest parent\u00a0<code class=\"em agj agk agl aga b\">package.json<\/code>.<\/li>\n<li id=\"8c1c\" class=\"ahd ahe abu ql b abv ahl aby ahm acb ahn acf aho acj ahp acn ahi ahj ahk dz bx\" data-selectable-paragraph=\"\">Files ending with\u00a0<code class=\"em agj agk agl aga b\">.cjs<\/code>\u00a0are always loaded as CommonJS regardless of the nearest parent\u00a0<code class=\"em agj agk agl aga b\">package.json<\/code>.<\/li>\n<\/ol>\n<h3 id=\"1fa8\" class=\"agn age abu bu wr ago agp agq pp agr ags agt pu pv agu pw pz qa agv qb qe qf agw qg qj agx bx\">But before you get overly optimistic\u2026<\/h3>\n<p>This only works when\u00a0<code class=\"em agj agk agl aga b\">await<\/code>\u00a0call is made at the top level of modules. Here\u2019s an illustration:<\/p>\n<p><img decoding=\"async\" alt=\"Blog image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1DV2USzEnMcGgr-6yHatTcQ.png\" \/><\/p>\n<blockquote><p><em><strong class=\"bd\">ECMAScript module loader is asynchronous<\/strong>. It means that top-level await enables the module to act like a BIG async function when NodeJs loads it as ES Module. Hence, the top-level await doesn\u2019t need an explicit async function wrapper. It also means that an await without async is a myth!<\/em><\/p><\/blockquote>\n<h3 id=\"085d\" class=\"aii age abu bu wr pl aij pm pp pq aik pr pu acb ail aim pz acf ain aio qe acj aip aiq qj air bx\">Disclaimer<\/h3>\n<p id=\"b9c7\" class=\"pw-post-body-paragraph abs abt abu ql b abv agy vz abx aby agz wc aca acb aha acd ace acf ahb ach aci acj ahc acl acm acn nd bx\" data-selectable-paragraph=\"\">This is purely a NodeJs feature. There is no concept of\u00a0<code class=\"em agj agk agl aga b\">.mjs<\/code>\u00a0and\u00a0<code class=\"em agj agk agl aga b\">.cjs<\/code>\u00a0in vanilla JavaScript.<\/p>\n<p data-selectable-paragraph=\"\">***<\/p>\n<h3 id=\"c411\" class=\"agn age abu bu wr ago aix agq pp agr aiy agt pu pv aiz pw pz qa aja qb qe qf ajb qg qj agx bx\">Note of thanks \u2764\ufe0f<\/h3>\n<p><img decoding=\"async\" alt=\"Blog image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/0XnuqCv7iw5_63Pgd.jpg\" \/><\/p>\n<p>Thank you for stopping by. Hope, you find this article helpful. Please <a class=\"ax aht\" href=\"https:\/\/medium.com\/@shiva.chaturvedi91\" rel=\"noopener\"><strong class=\"ql mx\"><em class=\"ahq\">follow me<\/em><\/strong><\/a>\u00a0on medium and help me reach 1k followers ??. That will truly encourage me to put out more content.<\/p>\n<blockquote><p><em>P.S.: If you feel something can be improved or lacks proper explanation, drop me a note in the comment box or mail me at\u00a0<a class=\"ax aht\" href=\"mailto:shiva.chaturvedi91@gmail.com\" target=\"_blank\" rel=\"noopener ugc nofollow\">shiva.chaturvedi91@gmail.com<\/a>. After all, you can teach me a thing or two as well.<\/em><\/p><\/blockquote>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) =&gt; { setTimeout(() =&gt; { resolve({ name: &#8220;Shivang&#8221;, id: &#8220;asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku&#8221;, }); }); }); } \/\/ Used an async wrapper function to make an await call (async () =&gt; { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you [&hellip;]<\/p>\n","protected":false},"author":1535,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":495,"footnotes":""},"categories":[4684,1185],"tags":[5084,5093,1787,229,55,1177,1472],"class_list":["post-56521","post","type-post","status-publish","format-standard","hentry","category-mean-2","category-node-js-2","tag-backend","tag-computer-science","tag-development","tag-services","tag-javascript","tag-nodejs-2","tag-programming"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) =&gt; { setTimeout(() =&gt; { resolve({ name: &quot;Shivang&quot;, id: &quot;asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku&quot;, }); }); }); } \/\/ Used an async wrapper function to make an await call (async () =&gt; { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Shivang Chaturvedi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/\" \/>\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=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Top level await() without async() function in NodeJs | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) =&gt; { setTimeout(() =&gt; { resolve({ name: &quot;Shivang&quot;, id: &quot;asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku&quot;, }); }); }); } \/\/ Used an async wrapper function to make an await call (async () =&gt; { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Top level await() without async() function in NodeJs | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) =&gt; { setTimeout(() =&gt; { resolve({ name: &quot;Shivang&quot;, id: &quot;asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku&quot;, }); }); }); } \/\/ Used an async wrapper function to make an await call (async () =&gt; { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\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.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#article\",\"name\":\"Top level await() without async() function in NodeJs | TO THE NEW Blog\",\"headline\":\"Top level await() without async() function in NodeJs\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/shivang-chaturvedi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"\\\/blog\\\/wp-ttn-blog\\\/uploads\\\/2024\\\/01\\\/0uJsNKFahmenV5AhB.jpg\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#articleImage\"},\"datePublished\":\"2023-01-24T21:15:57+05:30\",\"dateModified\":\"2024-01-02T17:41:49+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#webpage\"},\"articleSection\":\"MEAN, Node.js, Backend, Computer Science, Development, grails services, javascript, nodejs, programming\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"position\":2,\"name\":\"Node.js\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#listItem\",\"name\":\"Top level await() without async() function in NodeJs\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#listItem\",\"position\":3,\"name\":\"Top level await() without async() function in NodeJs\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/shivang-chaturvedi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/shivang-chaturvedi\\\/\",\"name\":\"Shivang Chaturvedi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/781d8391-ed8b-4046-b370-50390c28c0c1_Shivang-Chaturvedi-Profile-Pitcure.jpeg\",\"width\":96,\"height\":96,\"caption\":\"Shivang Chaturvedi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/\",\"name\":\"Top level await() without async() function in NodeJs | TO THE NEW Blog\",\"description\":\"Here\\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) => { setTimeout(() => { resolve({ name: \\\"Shivang\\\", id: \\\"asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku\\\", }); }); }); } \\\/\\\/ Used an async wrapper function to make an await call (async () => { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/top-level-await-without-async-function-in-nodejs\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/shivang-chaturvedi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/shivang-chaturvedi\\\/#author\"},\"datePublished\":\"2023-01-24T21:15:57+05:30\",\"dateModified\":\"2024-01-02T17:41:49+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Top level await() without async() function in NodeJs | TO THE NEW Blog","description":"Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) => { setTimeout(() => { resolve({ name: \"Shivang\", id: \"asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku\", }); }); }); } \/\/ Used an async wrapper function to make an await call (async () => { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you","canonical_url":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#article","name":"Top level await() without async() function in NodeJs | TO THE NEW Blog","headline":"Top level await() without async() function in NodeJs","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/shivang-chaturvedi\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/0uJsNKFahmenV5AhB.jpg","@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#articleImage"},"datePublished":"2023-01-24T21:15:57+05:30","dateModified":"2024-01-02T17:41:49+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#webpage"},"articleSection":"MEAN, Node.js, Backend, Computer Science, Development, grails services, javascript, nodejs, programming"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/node-js-2\/#listItem","position":2,"name":"Node.js","item":"https:\/\/www.tothenew.com\/blog\/category\/node-js-2\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#listItem","name":"Top level await() without async() function in NodeJs"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#listItem","position":3,"name":"Top level await() without async() function in NodeJs","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/shivang-chaturvedi\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/shivang-chaturvedi\/","name":"Shivang Chaturvedi","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/781d8391-ed8b-4046-b370-50390c28c0c1_Shivang-Chaturvedi-Profile-Pitcure.jpeg","width":96,"height":96,"caption":"Shivang Chaturvedi"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/","name":"Top level await() without async() function in NodeJs | TO THE NEW Blog","description":"Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) => { setTimeout(() => { resolve({ name: \"Shivang\", id: \"asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku\", }); }); }); } \/\/ Used an async wrapper function to make an await call (async () => { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/shivang-chaturvedi\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/shivang-chaturvedi\/#author"},"datePublished":"2023-01-24T21:15:57+05:30","dateModified":"2024-01-02T17:41:49+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Top level await() without async() function in NodeJs | TO THE NEW Blog","og:description":"Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) =&gt; { setTimeout(() =&gt; { resolve({ name: &quot;Shivang&quot;, id: &quot;asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku&quot;, }); }); }); } \/\/ Used an async wrapper function to make an await call (async () =&gt; { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you","og:url":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Top level await() without async() function in NodeJs | TO THE NEW Blog","twitter:description":"Here\u2019s how async-await function typically works: function myDummyBackendCall() { return new Promise((resolve) =&gt; { setTimeout(() =&gt; { resolve({ name: &quot;Shivang&quot;, id: &quot;asmdhajkdhajdsh8qweuqoweuiqepoi-0q-0eueiuaisjdaKcjaisku&quot;, }); }); }); } \/\/ Used an async wrapper function to make an await call (async () =&gt; { let data = await myDummyBackendCall(); console.log(data); })(); If you are a JS developer, you","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"56521","title":null,"description":null,"keywords":[],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"#aioseo-article-65e04efd64fdc","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","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":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"limit_modified_date":false,"created":"2023-01-24 15:43:53","updated":"2024-02-29 09:31:41","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"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.tothenew.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.tothenew.com\/blog\/category\/node-js-2\/\" title=\"Node.js\">Node.js<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tTop level await() without async() function in NodeJs\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Node.js","link":"https:\/\/www.tothenew.com\/blog\/category\/node-js-2\/"},{"label":"Top level await() without async() function in NodeJs","link":"https:\/\/www.tothenew.com\/blog\/top-level-await-without-async-function-in-nodejs\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1535"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=56521"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56521\/revisions"}],"predecessor-version":[{"id":59752,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56521\/revisions\/59752"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=56521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=56521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=56521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}