title: how to interrupt timeout long running method in nodejs?
body: <p>Using the languages with built-in support for threads this seems to be be a fairly easy task but,</p>
  
  <p>Is there any way to terminate/timeout a long running method / infinitely recursive method ?</p>
  
  <p>For instance If I have an api that takes callback as argument, and  does some processing before and after it's invocation and returns the result.</p>
  
  <pre><code>function api(data,callback) {
    processedData = before(data);
    result = callback(processedData);
    processedResult = after(result);
    return processedResult;
  }
  </code></pre>
  
  <p>If the notorious developer calls this api with following callback</p>
  
  <pre><code>function cpuFrenzy(data) {
    while(true);
  } 
  
  result = api( {} , cpuFrenzy);
  render(result);
  </code></pre>
  
  <p>and result of api is used to render HTML.</p>
  
  <p>Node js being single threaded would block all requests to the server !</p>
  
  <p>So is there any way for the api to timeout / terminate the callback method execution if it takes beyond some preset time, in order to avoid blocking all web requests?</p>
  
  <p>If there is none, is there any other way to implement this api ?</p>