可以等待ES6进口吗?

时间:2018-02-05 11:38:17

标签: javascript typescript import ecmascript-6

我看到this answer关于es6进口与要求之间的差异 但其中一个答案引起了我的注意:

  

事情import()实际上是异步的。

示例:

const module = await import('./module.js');

mdn says nothing about it.

我一直在使用进口已有一段时间,但从未知道可以等待它?

我还创建了一个实验(虚拟角度测试):

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)
  }
}

enter image description here

所以导入似乎会返回一个承诺?

问题

我错过了什么?我没有必要在导入Component的前两行中等待:

import { Component } from '@angular/core'; //no await

@Component({       
 ...
})

它在哪里说导入会返回一个承诺?

Also , one of the webpack programmers : (or system.js) said :

  

如果import只是一个异步函数,那么它将不再存在   可以静态分析模块,就像CommonJS一样   要求,我可以在任意条件下导入等。

But from my testing it does require await , which makes it asynchronisly evaluated.

那么这里发生了什么?

0 个答案:

没有答案
相关问题