Javascript check yield support

mck89 picture mck89 · Feb 19, 2010 · Viewed 11.1k times · Source

I read about the yield keyword in JavaScript and I need to use it in my project. I read that this keyword has been implemented starting from a certain version of JS so I think that old browsers don't support it (right?).

Is there a way to check if the yield keyword is supported? or at least is there a way to check if the version of JS is greater or equal than the one that implements that keyword (1.7)?

Answer

Tymon Sturgeon picture Tymon Sturgeon · Aug 30, 2011

Here's a function for checking if yield can be used.

var can_yield = (function(){ 
    try { 
        return eval("!!Function('yield true;')().next()"); 
    } 
    catch(e) { 
        return false; 
    } 
})();