提问人:Loopilla 提问时间:5/17/2023 最后编辑:Loopilla 更新时间:5/17/2023 访问量:470
如何将异步生成器函数定义为javascript类成员函数?
How can I define a async generator function as a javascript class member function?
问:
我有一个纯ES6+类。
我想定义一个异步生成器成员函数,例如:
class A { async *mygenerator() {} }
在打字稿中是可能的,但转译的代码并不是真正有用。
所以在打字稿中,这是有效的:
class A { public static async *myfunc<T>(): AsyncGenerator<T, void, void> {} }
也许这在纯 js 类中是不可能的......
--更新--
使用 linter 时,语法检查失败。
是否有任何 linter 选项可以防止将其解释为有效语法?
{
"extends": [
"eslint:recommended",
"plugin:node/recommended",
"plugin:import/recommended",
"prettier",
"airbnb-base"
],
"plugins": ["node", "prettier"],
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true,
"jest": true
},
"rules": {
"block-scoped-var": "error",
"eqeqeq": "error",
"no-var": "error",
"no-console": "error",
"prefer-const": "error",
"eol-last": "error",
"prefer-arrow-callback": "error",
"no-trailing-spaces": "error",
"quotes": ["warn", "single", { "avoidEscape": true }],
"no-restricted-properties": [
"error",
{
"object": "describe",
"property": "only"
},
{
"object": "it",
"property": "only"
}
],
"linebreak-style": 0,
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
],
"id-length": ["error", { "min": 2 }],
"comma-dangle": 0,
"max-params": ["error", 3],
"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }],
"import/no-unresolved": "off",
"node/no-missing-import": "off",
"import/prefer-default-export": "off",
"no-useless-constructor": "off",
"no-use-before-define": "off",
"import/extensions": "off",
"object-curly-newline": "off",
"no-underscore-dangle": "off",
"import/no-absolute-path": "off",
"node/no-unpublished-import": "off",
"import/no-extraneous-dependencies": "off",
"class-methods-use-this": "off",
"lines-between-class-members": "off",
"camelcase": "off",
"operator-linebreak": "off",
"implicit-arrow-linebreak": "off",
"function-paren-newline": "off",
"no-shadow": "off",
"indent": "off",
"brace-style": "off"
}
}
答:
0赞
Lajos Arpad
5/17/2023
#1
Javascript 异步生成器示例:
async function* foo() {
yield await Promise.resolve('a');
yield await Promise.resolve('b');
yield await Promise.resolve('c');
}
let str = '';
async function generate() {
for await (const val of foo()) {
str = str + val;
}
console.log(str);
}
generate();
Typescript 是一种强类型语言,Javascript 是一种弱类型语言。因此,您将无法像在 Typescript 中那样在 Javascript 中指定类型。但是,您可以在 Javascript 中创建异步生成器。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator
内部课堂
class YourClass {
async * yourMethod(msg) {
// Your code here
}
}
编辑
当然,异步生成器也可以是类成员
评论
1赞
Pointy
5/17/2023
这个问题是关于类成员函数的,但这个问题已经包含正确的解决方案,所以我不确定它是否重要。
1赞
Loopilla
5/18/2023
所以我根据 linter 错误禁用了一些 linter 选项,现在它是有效的。无论如何,我知道 js 生成器,但这是要修复的纯 javascript api,而不是我最近使用的打字稿。这就是为什么我不确定这是否有效。我的错。<!--语言: lang-js --> /* eslint-disable prettier/prettier / / eslint-disable no-restricted-syntax */ <!-- end snippet --> 感谢将我带到正确的道路上。下次我会在提出问题之前仔细考虑一下。
0赞
Lajos Arpad
5/18/2023
@Loopilla提出问题是完全有效的,即使后来证明我们本可以找到答案。事后看来,聪明总是很容易的,我也问过关于 SO 的问题,只是为了根据答案意识到我本可以弄清楚的。所以,请不要担心。我们没有弄清楚一切是正常的。
评论