Search code examples
javascriptbrowsergeneratorecmascript-harmonybrowser-feature-detection

How can I feature-detect ES6 generators?


I'm really enjoying ES6 generators. Is there a way I can detect generator support in browsers? I know generators might not be in a lot of browsers (or possible no browsers at all) at the moment, but that's OK for my purposes.

I tried:

try {
  function *(){}
} catch(err) {
  console.log("No generators");
}

But it doesn't seem to work.

How can I detect support for ES6 generators in browsers?


Solution

  • One of the few times that eval is actually the right solution.

    For language construct changes, you need something like this:

    try {
      eval("(function *(){})");
    } catch(err) {
      console.log(err);
      console.log("No generators");
    }