title: Use Promise to wait until polled condition is satisfied
body: <p>I need to create a JavaScript Promise that will not resolve until a specific condition is true.  Let's say I have a 3rd party library, and I need to wait until a certain data condition exists within that library.</p>
  
  <p>The scenario I am interested in is one where there is no way to know when this condition is satisfied other than by simply polling.</p>
  
  <p>I can create a promise that waits on it - and this code works, but is there a better or more concise approach to this problem?</p>
  
  <pre><code>function ensureFooIsSet() {
      return new Promise(function (resolve, reject) {
          waitForFoo(resolve);
      });
  }
  
  function waitForFoo(resolve) {
      if (!lib.foo) {
          setTimeout(waitForFoo.bind(this, resolve), 30);
      } else {
          resolve();
      }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>ensureFooIsSet().then(function(){
      ...
  });
  </code></pre>
  
  <p>I would normally implement a max poll time, but didn't want that to cloud the issue here.</p>