title: How can I run the same promise again depending on the result from the previous run?
body: <p>I have some code that is wrapped in a <code>promise</code>. This code goes and edits some records in a remote DB and returns with the number of edited records. If the number of edited records is larger than <code>0</code> (i.e., some work has been done), then I'd like to run the same method again and again until I get a <code>0</code> back (i.e., no more records need editing).</p>
  
  <p>I've tried some different ways, but they all do not terminate (i.e., the resolve is not called) or they don't run properly.</p>
  
  <p><strong>First version I tried:</strong></p>
  
  <pre><code>const doEdits = () =&gt; {
    return new Promise((resolve, reject) =&gt; {
      someDB.performEdits()
      .then(count =&gt; {
        if (count &gt; 0) {
          doEdits()
        } else {
          resolve()
        }
      })
      .catch(reject)
    })
  }
  
  return new Promise((resolve, reject) =&gt; {
    someDB.init()
    .then(doEdits)
    .catch(reject)
  })
  </code></pre>
  
  <p><strong>Second version I tried:</strong></p>
  
  <pre><code>const doEdits = () =&gt; {
    return new Promise((resolve, reject) =&gt; {
      someDB.performEdits()
      .then(count =&gt; {
        resolve(count)
      })
      .catch(reject)
    })
  }
  
  return new Promise((resolve, reject) =&gt; {
    someDB.init()
    .then(doEdits)
    .then(count =&gt; {
      if (count &gt; 0 ) {
        return doEdits()   // 'doEdits()' did not work...
      } else {
        resolve()
      }
    })
    .catch(reject)
  })
  </code></pre>
  
  <p>Any and all suggestions are welcome.</p>