title: Asynchronously call faceapi.detectAllFaces()
body: <p>I am using <a href="https://github.com/justadudewhohacks/face-api.js/" rel="nofollow noreferrer">face-api.js</a> to detect faces in a given image. Everything is configured as mentioned on Github and detection is working.</p>
  <p>But I want to call <code>faceapi.detectAllFaces()</code> asynchronously, I means I don't want to wait for its result, so I can't call <code>await faceapi.detectAllFaces()</code>. I tried below code:</p>
  <p>In cam.js</p>
  <pre><code>async function detectFace()
  {
      document.getElementById('camPic').src = getCurrentImageAsBase64(); //getCurrentImageAsBase64() gets the image in base64 format from canvas
      return faceapi.detectAllFaces(document.getElementById('camPic'), new faceapi.SsdMobilenetv1Options());  
  }
  </code></pre>
  <p>In index.jsp</p>
  <pre><code>$('#camPrcd').click(function(e)
  {
      e.preventDefault(); 
      detectFace().then((arr) =&gt; console.log(arr));
      console.log('detection called');
  });
  </code></pre>
  <p>After button click, on console I can see message &quot;detection called&quot; and after sometime detection result gets logged from <code>then()</code> block.</p>
  <p>But my observation is when <code>detectFace()</code> is called the html page feels like hanged (not able to click on any button) and when <code>arr</code> gets printed on console then I can able to click on page.</p>
  <p>Looks like even though &quot;detection called&quot; message gets printed immediately before detection happens but <code>faceapi.detectAllFaces()</code> is not doing work asynchronously.</p>
  <p><em>Note: This happens only for first call of <code>faceapi.detectAllFaces()</code> as per author of face-api.js during first call model for face detection get compiled and so detection time is more at first call as compare to subsequent call to function.</em></p>
  <p>So is there any way that, I can call for detection and still web page is accessible and when it's finished a callback function will handle the detection result.</p>