有什么方法可以在es模块中导出诺言的结果?

由于await仅可在异步函数内部使用,因此下面的代码不起作用。

const config_path = 'https://test.cdn.com/conf.' + location.hash.include('DEV')?'development.ts':'production.ts'
const promise = import(config_path)

export default config = await promise

那么有什么方法可以在es / ts模块中导出诺言的结果吗?

bigege 回答:有什么方法可以在es模块中导出诺言的结果?

deno可以支持top-level await

 await Promise.resolve(console.log('?'));
 const strings = await import(`/i18n/${navigator.language}`);

v8 / chrome的此功能正在开发中:https://v8.dev/features/top-level-await

,

来自docs,

“ await运算符用于等待Promise。它只能在异步函数中使用。

如果您打算在全局范围内使用异步等待,请使用see this question来解决。

本文链接:https://www.f2er.com/2759960.html

大家都在问