{"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\" 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\" 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\" 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\" 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\" 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\" 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":454},"categories":[4684,1185],"tags":[5084,5093,1787,229,55,1177,1472],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56521"}],"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}]}}