DEV Community

HandsomeTan
HandsomeTan

Posted on

static and dynamic import directive

in Javascript, import directive has two ways for loading external scripts. The first is static import:import { exports.name } from outside.js that is an async approach, but only in loading phase. It is a sync approach during the execution phase and will block Javascript main thread. Note: static import directive can only be declared at the top level of the module.
The second is dynamic: import(url).then
().catch()
that can be used anywhere you want. import() function returns a promise so its behavior is same to promise, which is executed in async method.

Top comments (0)