Example 1 — await import()
Inside async functions.
JavaScript
async function load() {
const mod = await Promise.resolve({ value: 7 });
console.log(mod.value);
}
load(); How It Works
- await waits for the module.
import(path) returns a Promise that resolves to a module namespace object. Use it for code-splitting and conditional loading.
Promise.resolve({ default: 1, hello: () => "hi" })
.then((mod) => {
console.log(mod.hello());
}); Inside async functions.
async function load() {
const mod = await Promise.resolve({ value: 7 });
console.log(mod.value);
}
load(); Import only when needed.
const need = true;
if (need) console.log("import('./heavy.js')"); Bundlers and browsers resolve differently.
console.log("use relative or absolute URLs"); CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful