From e9c3f2fbf4821737fa14735eba68c28a46edbbea Mon Sep 17 00:00:00 2001 From: Albus Dumbledore Date: Sat, 25 Apr 2020 20:19:05 +0530 Subject: [PATCH] docs(top-level-await): improve template.md --- examples/top-level-await/template.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/top-level-await/template.md b/examples/top-level-await/template.md index 80914eb0a..3da83813a 100644 --- a/examples/top-level-await/template.md +++ b/examples/top-level-await/template.md @@ -1,5 +1,5 @@ -Let's use `await` at top level in a module `db-connection.js`. -This makes sense since the connection to the DB need to established before the module is usable. +Let's use `await` at the top level in a module `db-connection.js`. +This makes sense since the connection to the DB needs to be established before the module is usable. # db-connection.js @@ -10,15 +10,15 @@ _{{db-connection.js}}_ But `db-connection.js` is no longer a normal module now. It's an **async module** now. Async modules have a different evaluation semantics. -While normal modules evaluate in a synchronous way, async modules evaluate in an asynchronous way. +While normal modules evaluate synchronously, async modules evaluate asynchronously. -Async modules can't imported with a normal `import`. +Async modules can't be imported with a normal `import`. They need to be imported with `import await`. The main reason for this is to make the using module aware of the different evaluation semantics. Using `import await` in a module also makes the module an async module. -You can see it as form of top-level-await, but it's a bit different because imports hoist, so does `import await`. +You can see it as a form of top-level-await, but it's a bit different because imports hoist, so does `import await`. All `import`s and `import await`s hoist and are evaluated in parallel. `import await` doesn't affect tree shaking negatively. @@ -35,7 +35,7 @@ Now it looks like that this pattern will continue and will infect all using modu Yes, this is kind of true and makes sense. All these modules have their evaluation semantics changed to be async. -But you as developer don't want this. +But you as a developer don't want this. You want to break the chain at a point in your module graph where it makes sense. Luckily there is a nice way to break the chain. @@ -62,12 +62,12 @@ _{{example.js}}_ ``` Note that you may `import await` from a normal module too. -This is legal, but mostly unneeded. -`import await` may also been seen by developers as hint that this dependency does some async actions and may delay evaluation. +This is legal, but mostly not required. +`import await` may also be seen by developers as a hint that this dependency does some async actions and may delay evaluation. -As guideline you should prevent your application entry point to become an async module when compiling for web targets. +As a guideline, you should prevent your application entry point to become an async module when compiling for web targets. Doing async actions at application bootstrap will delay your application startup and may be negative for UX. -Use `import()` to do async action on demand or in background and use spinners or other indicators to inform the user about background actions. +Use `import()` to do async action on-demand or in the background and use spinners or other indicators to inform the user about background actions. When compiling for other targets like node.js, electron or WebWorkers, it may be fine that your entry point becomes an async module.