美文网首页
top-level await

top-level await

作者: 努力学习的小丸子 | 来源:发表于2021-08-04 23:41 被阅读0次

https://github.com/tc39/proposal-top-level-await
含义:允许在模块的顶层独立使用await命令。

// index.js 以下写法不会报错
const data = await fetch('https://api.example.com');

使用场景

// import() 方法加载
const strings = await import(`/i18n/${navigator.language}`);
// 数据库操作
const connection = await dbConnector();
// 依赖回滚
let jQuery;
try {
  jQuery = await import('https://cdn-a.com/jQuery');
} catch {
  jQuery = await import('https://cdn-b.com/jQuery');
}

相关文章

网友评论

      本文标题:top-level await

      本文链接:https://www.haomeiwen.com/subject/iorwmltx.html