I saw this answer about differences between es6 imports vs require.
But one of the answers there caught my eyes:
Thing is
import
() is actually async in nature.
Example :
const module = await import('./module.js');
I've been using imports for some time now but never knew that it can be awaited ??
I've also created an experiment (dummy angular test):
1.ts
export const a=3;
app.component.ts
import { Component } from '@angular/core'; //no await here
@Component({
...
})
export class AppComponent {
constructor() {
this.foo();
}
async foo() {
const module = await import('./1'); //<--- if I remove the await, it doesn't work
alerqt(module.a)
}
}
So it seems that import returns a promise ?
Question
What am I missing? I didn't have to await in the first two lines of where I imported Component:
import { Component } from '@angular/core'; //no await
@Component({
...
})
And where does it say that import returns a promise ?
Also , one of the webpack programmers : (or system.js) said :
If import was just an asynchronous function, then it would no longer be possible to statically analyze modules, as just like CommonJS require, I could have import within arbitrary conditions etc.
But from my testing it does require await , which makes it asynchronisly evaluated.
So what is going on here?