title: Node.js call async function from within do while loop
body: <p>I want to call an async function from within a do while loop and exit only if the function doesn't return a value. 
  Is there a way to manage this without having to actually <em>wait</em> outside the callback?</p>
  
  <p>Here's the code that obviously always exits in the first loop since the function hasn't called back yet, when the while () is evaluated:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var i = 0;
  do {
    var foundListing;
    if (i) {
      listing.slug = listing.slug + '-' + (i + 1);
    }
    listingService.getListingBySlug(listing, function(error, pFoundListing) {
      if (error) {
        return callback(util.errorParser(error));
      } else if (Object.keys(pFoundListing).length) {
        foundListing = pFoundListing;
        i++;
      }
    });
    //Do I have to wait here???
  } while (foundListing &amp;&amp; Object.keys(foundListing).length);</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>For Clarification:</strong>
  The point here is to generate a unique slug. If the slug already exists, i append a number and check again if it exists. When I get to the number that doesn't exist yet, I'm done.</p>
  
  <p><strong>Update:</strong>
  I found a way using recursion. I posted the working snippet as an <a href="https://stackoverflow.com/a/41695785/2639357">answer</a>.</p>