- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Dynamic Import
JavaScript Modules
JavaScript Dynamic Import
import()loads a module while the program is running, returning a promise.
The static import loads everything up front.
import() loads only when you actually call it.
Example
Example
javascript
// Simulating a dynamic import with a promise, since there is
// no second file to load here.
function importMathModule() {
return Promise.resolve({
double: function (n) { return n * 2; }
});
}
importMathModule().then(function (module) {
console.log(module.double(5));
});