Skip to content

Commit a66f514

Browse files
committed
minor
1 parent 5173faa commit a66f514

File tree

1 file changed

+14
-16
lines changed
  • 1-js/13-modules/03-modules-dynamic-imports

1 file changed

+14
-16
lines changed

1-js/13-modules/03-modules-dynamic-imports/article.md

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Dynamic imports
22

3-
Export and import statements that we covered in previous chapters are called "static".
4-
5-
That's because they are indeed static. The syntax is very strict.
3+
Export and import statements that we covered in previous chapters are called "static". The syntax is very simple and strict.
64

75
First, we can't dynamically generate any parameters of `import`.
86

@@ -24,27 +22,27 @@ if(...) {
2422
}
2523
```
2624

27-
That's because `import`/`export` aim to provide a backbone for the code structure. That's a good thing, as code structure can be analyzed, modules can be gathered and bundled together, unused exports can be removed ("tree-shaken"). That's possible only because the structure of imports/exports is simple and fixed.
25+
That's because `import`/`export` aim to provide a backbone for the code structure. That's a good thing, as code structure can be analyzed, modules can be gathered and bundled into one file by special tools, unused exports can be removed ("tree-shaken"). That's possible only because the structure of imports/exports is simple and fixed.
2826

2927
But how can we import a module dynamically, on-demand?
3028

31-
## The import() function
29+
## The import() expression
3230

33-
The `import(module)` function can be called from anywhere. It returns a promise that resolves into a module object.
31+
The `import(module)` expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can be called from any place in the code.
3432

35-
The usage pattern looks like this:
33+
We can use it dynamically in any place of the code, for instance:
3634

37-
```js run
38-
let modulePath = prompt("Module path?");
35+
```js
36+
let modulePath = prompt("Which module to load?");
3937

4038
import(modulePath)
4139
.then(obj => <module object>)
42-
.catch(err => <loading error, no such module?>)
40+
.catch(err => <loading error, e.g. if no such module>)
4341
```
4442

4543
Or, we could use `let module = await import(modulePath)` if inside an async function.
4644

47-
For instance, if we have the following `say.js`:
45+
For instance, if we have the following module `say.js`:
4846

4947
```js
5048
// 📁 say.js
@@ -75,12 +73,12 @@ export default function() {
7573
}
7674
```
7775

78-
...Then, in order to access it, we can use `default` property of the module object, as explained in the [previous chapter](info:import-export).
79-
80-
So, the dynamic import will be like this:
76+
...Then, in order to access it, we can use `default` property of the module object:
8177

8278
```js
83-
let {default: say} = await import('./say.js'); // save .default property in say variable
79+
let obj = await import('./say.js');
80+
let say = obj.default;
81+
// or, in one line: let {default: say} = await import('./say.js');
8482

8583
say();
8684
```
@@ -96,5 +94,5 @@ Dynamic imports work in regular scripts, they don't require `script type="module
9694
```smart
9795
Although `import()` looks like a function call, it's a special syntax that just happens to use parentheses (similar to `super()`).
9896
99-
So we can't copy `import` to a variable or use `.call/apply` with it.
97+
So we can't copy `import` to a variable or use `.call/apply` with it. That's not a function.
10098
```

0 commit comments

Comments
 (0)