Poll for a result n times (with delays between attempts) before failing
<p>We need to write a Node.js function that polls a certain API endpoint for a result of a previously requested calculation.  The result takes a random time to be generated and may not me generated at all.  We'd like to get it as soon as possible, but also I don't want to wait for too long, which means that after a certain number of API calls we'd like the function to fail (reject a Promise).</p>

<p>There is one way communication between our code and the API.</p>

<pre><code>const Bluebird = require('bluebird');

function getResult() {
  return new Bluebird(async function (resolve, reject) {

    let counter = 0;

    while (counter &lt; 10) {
      await Bluebird.delay(1000);

      const res = await apiCall();
      if (res.data) {
        resolve(res.data);
      } else {
        counter += 1;
      }
    }

    reject('timeout');
  });
}
</code></pre>

<p>Is this the correct approach?</p>