我正在寻找一种在代码中查找位置的方法(比如在Atom中使用jslint进行验证),其中函数是用*定义的,但是在调用它时没有指定“yield”。我经常忘记“屈服”,并希望能为我提醒/验证这一点。有办法吗? Atom包可能吗?
举例说明和示例:
let f = function* () {
yield doAsyncStuff();
yield doMoreAsyncStuff();
let res = yield fetchAsyncStuff();
return res;
}
let caller = function* () {
yield anotherFunction();
let res = x(); // <-- here I have missed the yield
}
答案 0 :(得分:3)
Eslint有一条名为require-yield
的规则,用于检测生成器函数中是否使用了yield
个关键字。但是在您的caller
函数中,您在调用yield
时已经有一个anotherFunction
,并且因为在调用yield
函数时未使用x
可能是逻辑的一部分在你的代码背后,没有合理的方法来检测那里丢失的yield
顺便说一下,这个问题已被讨论过,而且不可行:Rule Idea: Ensure yield on call to generator function