Shantun's Blog

For what reason Should You Use Top-level Await in JavaScript?

Published 3 months ago5 min read4 comments
image

Before the presentation of high level anticipate, in the event that you actually attempt to utilize the anticipate watchword outside of an async work, you would get a Syntax mistake. To stay away from this, engineers utilized Immediately Invoked Function Expressions(IIFEs).

Be that as it may, the above case and arrangement is only a glimpse of something larger.

At the point when you work with ES6 modules, there will be plentiful occasions where you will trade esteems and bringing in them. We should take a gander at such a model. 

In the above model, we fare and import factors among library.js and middleware.js . You can name the documents in any capacity you need.

 

In the event that you cautiously see, you would see a delay function that essentially restores a Promise which would resolve after a break. Since it is nonconcurrent, we utilize the await catchphrase inside our async IIFE to stand by until the execution finishes. In a true model, the guarantee would be supplanted by a get call or an errand of nonconcurrent nature. When the guarantee is settled, we appoint qualities to our factors which are processed from the capacities imported from library.js .

This basically means that our variables are undefined until the promise gets resolved.

When you have a look at the end of the above code snippet, you will see that the variables we have calculated are being exported, so that another module can use them.

Let’s look at a module that imports and uses the above-exported variables.

If you run the above code snippet, you will notice that the first two log statements are undefined and the latter print values 169 and 13. How does this happen?

This is because the exports from the module middleware.js are accessed by main.js before the completion of the async function. Remember we had a promise waiting to be resolved…

To solve this problem, we should somehow notify the modules importing these variables when it is ready to access them.

Workarounds

There are two commonly used workarounds available for the above problem.

1.Export a Promise to represent initialization

You can export the IIFE and can use that to identify when the exports are ready to be accessed. The async keyword makes a method asynchronous, which in turn always returns a promiseThis is why the async IIFE returns a promise in the below solution.

When you access these exports from main.js , you can wait for the async IIFE to resolve and then access the variables.

Although the above solution does the job, it introduces some new problems.

  • Everyone should start following this pattern as a standard as you have to find the right promise to wait.
  • If another module depends on the variables squareOutput and diagonalOutput from main.js , we should make sure we re-export the IIFE promise as well. So that the other module too knows when to access our variables.

There is another workaround that solves the above-said issues.

2.Resolve the IIFE promise with the variables that should be exported

In this workaround, rather than exporting the variables separately, we return them from our async IIFE. This allows our main.js file to simply wait for the promise to resolve and retrieve the value.

But this solution too has its own set of complications.

According to the proposal, “this pattern has the undesirable effect of requiring a broad reorganization of the related source into more dynamic patterns and placing much of the module body inside the .then() callback in order to use the dynamically available imports. This represents a significant regression in terms of static analyzability, testability, ergonomics, and more, compared to ES2015 modules”.

Promo Section Heading

You can use this section to promote your side projects etc. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.

image
Choose Colour