The `import()` Syntax of ES2020 Looks Promising; How to Detect if Such Feature Available Then?

taufik_nurrohman

Taufik Nurrohman

Posted on June 20, 2020

The `import()` Syntax of ES2020 Looks Promising; How to Detect if Such Feature Available Then?

With the plan, import() is proposed to work in both modules and scripts, giving script code an easy asynchronous entry point into the module world and allowing it to start running module code.

https://www.infoworld.com/article/3538809/ecmascript-2020-spec-for-javascript-approved.amp.html

Can I check the existence of this feature as it was a function? And if that is possible, is it conform to the standard?

What I am thinking:

if ('function' === typeof import) {
    import('taufik-nurrohman/f3h.js').then(F3H => {
        let f3h = new F3H;
    });
}
Enter fullscreen mode Exit fullscreen mode

Also, how do I expose module in the file to import, without causing syntax errors in the window environment?

// :|
if ('function' === typeof import) {
    export default F3H;
} else {
    window.F3H = F3H;
}
Enter fullscreen mode Exit fullscreen mode

Thanks!

💖 đŸ’Ș 🙅 đŸš©
taufik_nurrohman
Taufik Nurrohman

Posted on June 20, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related