title: How to set a time limit to a method in NodeJs?
body: <p>I have a use case, where I am doing an external API call from my code,<br>
  The response of the external API is required by my code further on<br><br>
  I am bumping into a scenario, where the external API call at times takes far too long to return a response,<br>
  casing my code to break, being a serverless function<br></p>
  <p>So I want to set a time limit to the external API call,<br>
  Where if I don't get any response from it within 3 secs, I wish the code to gracefully stop the further process<br></p>
  <p>Following is a pseudo-code of what I am trying to do, but couldn't figure out the logic -<br></p>
  <pre><code>let test = async () =&gt; {
      let externalCallResponse = '';
  
      await setTimeout(function(){ 
  
          //this call sometimes takes for ever to respond, but I want to limit just 3secs to it
          externalCallResponse = await externalCall();
      }, 3000);
  
      if(externalCallResponse != ''){
          return true;
      }
      else{
          return false;
      }
  }
  
  test();
  </code></pre>
  <p><strong>Reference -</strong><br>
  <a href="https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#getParameters-property" rel="nofollow noreferrer">https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#getParameters-property</a><br>
  I'm using AWS SSM's getParameters method</p>