# ECMAScript

# 2021

  • String.prototype.replaceAll: 展示 'hello world'.replaceAll('l', 'L') // 'heLLo worLd'
  • Promise.any: 返回第一个获得的 promiseAggregateError
try {
  const first = await Promise.any(promises);
  // Any of the promises was fulfilled.
} catch (error) {
  // All of the promises were rejected.
}
1
2
3
4
5
6
名称 描述 备注
Promise.allSettled 不短路 在 ES2020 中添加 ✅
Promise.all 输入值被拒绝时发生短路 在 ES2015 中添加 ✅
Promise.race 输入值稳定后发生短路 在 ES2015 中添加 ✅
Promise.any 满足输入值时发生短路 这个建议 🆕 预定于 ES2021
a ||= b
a || (a = b)

a &&= b
a && (a = b)

a ??= b
a ?? (a = b)
1
2
3
4
5
6
7
8
  • Numeric separators: 1_000_000_000, 1_123_123_321.38

# 2020

const promises = [fetch('index.html'), fetch('https://does-not-exist/')]

const results = await Promise.allSettled(promises);
const errors = results
  .filter(p => p.status === 'rejected') // 收集错误的回调
  .map(p => p.reason);
1
2
3
4
5
6