使用Async Iteration在TypeScript中定位ES5并在浏览器上运行时出错

时间:2017-09-08 19:03:27

标签: typescript

关于ES4"的"生成和迭代the docs后,我添加了以下polyfill:

(Symbol as any).asyncIterator = Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");

这会导致我的浏览器抛出错误:

Uncaught TypeError: Cannot assign to read only property ‘asyncIterator’ of function ‘function Symbol() { [native code] }’

1 个答案:

答案 0 :(得分:2)

由于该属性是只读的,因此只有在它不存在时才分配:

if (typeof (Symbol as any).asyncIterator  === 'undefined') {
 (Symbol as any).asyncIterator = Symbol.asyncIterator || Symbol('asyncIterator');
}