How does lazy module loading work in ES6 How does lazy module loading work in ES6 javascript javascript

How does lazy module loading work in ES6


The import statement will only work in the very top of files, and it will load all of them. This is mainly to avoid potential issues with circular dependencies.

There will also be a way to do asynchronous loading; however the specification doesn't seem to be finalized yet. The ES6 module loader polyfill package uses a method called System.import(moduleName) that returns a promise and the final specification is likely to look similar:

function someEventHandler() {    System.import('some-module').then((SomeModule) => {        var module = new SomeModule();        // ...    })}


Example for lazyloading jQuery in ES6 :

import('jquery').then((jquery) => {     // Because jquery is the module, here is the new syntax for using this           window.$ = jquery.default;          window.$('#id');});