Search code examples
javascriptasynchronousyield

Find missing yield statements


I'm looking for a way to find places in code (like validating with jslint in Atom) where function is defined with *, but when calling it has no "yield" specified. I forgetting to "yield" too often and would like something to remind/validate this for me. Is there a way? Atom Package maybe?

To illustrate and example:

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
}

Solution

  • Eslint has a rule called require-yield which detects if there is no yield keyword used in a generator function. BUT in your caller function, you already have one yield when calling anotherFunction, and since not using yield when calling x function could be part of the logic behind your code, there is no logical way to detect the missing yield there.
    By the way this issue is pretty much discussed and not feasible: Rule Idea: Ensure yield on call to generator function