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 non concurrent 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 promise. This 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
anddiagonalOutput
frommain.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 tackles the above-said issues.
2.Resolve the IIFE guarantee with the factors that ought to be exported
In this workaround, as opposed to sending out the factors independently, we return them from our async IIFE. This permits our main.js
file to just trust that the guarantee will resolve and recover the value.
But this arrangement also has its own arrangement of complications.
According to the proposition, “this example has the bothersome impact of requiring a wide redesign of the connected source into more powerful examples and setting a significant part of the module body inside the .then()
callback to utilize the progressively accessible imports.
This addresses a critical relapse regarding static investigate capacity, test capacity, ergonomics, and the sky is the limit from there, contrasted with ES2015 modules”.