April 2020
Intermediate to advanced
716 pages
18h 55m
English
Dynamic import() is a function-like version of the regular import, and it enables the dynamic loading of JS modules. Using import(moduleSpecifier) will return a promise for the module namespace object of the requested module. When using regular static imports, we import a module at the top of the code and then use it in the code as follows:
import { convert } from './metric'...console.log(convert('km', 'miles', 202))
In contrast, if we were to use dynamic import() instead of adding the static import at the beginning, the code would look like this:
import('./metric').then({ convert } => { console.log( convert('km', 'miles', 202) ) })
This allows us to import and load the module when the code requires it. While bundling the ...