try {
requestAnimationFrame(function re(){
re();
})}
catch (e){
console.log(e);
}
I tried above code in console, i
This can be certainly done. More than that – if you'll stick to a meaningful pattern, you can determine how many times your function had run before the call stack overflowed (I wouldn't dare to try it on a function that is recursively invoked, rather than returned, as in the original question, because in this case the call stack can get really funky):
function doRecursionOrDieTryin(n = 0) {
try {
return doRecursionOrDieTryin(n + 1);
} catch(err) {
return `There was an exception: "${err}" when tryin' to make iteration no. ${n}`;
}
}
doRecursionOrDieTryin()
Warning: This function starts an infinite loop, so it could crash you browser if you will try to run this in a distant future, when the common JS engines will be fully tail-call-optimized. For 10/2018, it gots to iteration no. 10454 in Chrome 69. In Firefox 62 the limit of stack overfow is changing (from 10077 to 20989, then 22160), probably due to some kind of smart runtime optimization. Haven't test it yet in Edge.