velvetkevorkian
velvetkevorkian

Reputation: 790

Does `import()` resolve when the module is fully parsed?

Using the dynamic import() syntax, I can load a module at runtime, e.g.

if (someCondition) await import('some-other-file');

Is it safe to assume that, once that promise resolves, some-other-file has been fully parsed, and any synchronous side-effects it triggers will have happened already?

Upvotes: 2

Views: 57

Answers (1)

Guerric P
Guerric P

Reputation: 31815

Yes this is the actual behavior, as written in MDN Docs

Demo with a data URL:

const code = "window.foo = 'bar';";
const importString = `data:application/javascript;charset=utf-8;base64,${btoa(code)}`;

import(importString).then(() => console.log(foo))

Upvotes: 1

Related Questions