Skip to content

Instantly share code, notes, and snippets.

@g-i-o-
Created August 18, 2016 15:30
Show Gist options
  • Select an option

  • Save g-i-o-/ca668f0968c6f57e96cc677205f31b73 to your computer and use it in GitHub Desktop.

Select an option

Save g-i-o-/ca668f0968c6f57e96cc677205f31b73 to your computer and use it in GitHub Desktop.
Returns the call stack of the function calling this function.
/** Returns the call stack of the function calling this function.
* @return {Array} of function calls up to the function calling this function
*/
function getCallStack(){
try{
throw new Error('! ... Snake!, SNAKE!!!');
} catch(e){
return (e.stack || '').split('\n').filter(function(_){
return /^\s*at\s+.+/.test(_);
}).map(function(_){
return /^\s*at\s+(.+)/.exec(_)[1];
}).filter(function(_, i){
return i > 1; // ignore both the error throw, and the call to this function.
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment