Dynamic import()
Dynamic import() loads a module asynchronously and returns a Promise for its module namespace object. It is useful for lazy loading, conditional functionality, and reducing initial application work.
Unlike a static import declaration, dynamic import can be placed inside expressions and functions.
Lazy Loading
async function openEditor() {
const module = await import('./editor.js');
module.startEditor();
}
openEditor();The editor module is requested only when openEditor runs. The Promise resolves to the module namespace.
Key Takeaways
dynamic import returns a Promise.
It enables lazy loading.
It can reduce initial loading work in large applications.
Knowledge Check
What does dynamic import() return?