我有一个TS类,它将以下异步生成器定义为公共访问器:
/**
* Provides a way to iterate through the templates, one
* channel at a time.
*/
public async *iterator() {
let output: IDictionary = {};
const substitutions = this.isListDataset
? this._substitutions
: [this._substitutions];
yield this._channels.map(async function*(channel) {
const fn = await this.compileTemplate(this._topic, channel);
const output: ITemplateChannel = {
channel,
templates: []
};
this._substitutions.map((data: IDictionary) => {
output.templates.push(fn(data));
});
yield output;
});
}
编辑器( vs-code )对此声明非常满意,但如果我在类定义上运行ts-node
,则会抱怨:
意外的令牌*
好的,我知道库支持asyncIterators和生成器需要配置,但我的tsconfig.json
是:
{
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"target": "esnext",
"lib": ["esnext.asynciterable", "esnext", "es2015.generator"],
"moduleResolution": "node",
"sourceMap": true,
"noImplicitAny": true,
"outDir": "./lib",
"removeComments": true,
"typeRoots": ["./node_modules/@types"]
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*-spec.ts"]
}
我错过了什么?
注意:我注意到,如果我将yield output
更改为yield* output
,我猜它应该 ... vs-code 指出:
好的,现在我读到了这种依赖,但我不明白我将如何实现它。我失败的尝试是将此方法添加到类中:
public [Symbol.asyncIterator]() {
return Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");
}
但猜测我需要将它映射到map
函数?是对的吗?或者iterator
方法本身?可能两者?困惑。