title: for..in loop for asynchronous tasks in Javascript
body: <p>I have an associative container (key/value, "hash"). I iterate over it using the for..in loop:</p>
  
  <pre><code>for (key in container) {
     doSomethingWith(key, container[key]
  }
  </code></pre>
  
  <p>Now I want doSomethingWith() to return a promise (Q or the standard one), and I want to iterate sequentially, not in parallel. That is, I want every call of doSomethingWith() to resolve its promise before calling it again.</p>
  
  <p>How do I do it? </p>
  
  <ol>
  <li><p>Is there a way to iterate over keys without resorting to Object.keys() (which in my case is pretty long) and indices? Are new Map class helpful?</p></li>
  <li><p>It may be already written by someone. Also, if I want to have a limited concurrency (e.g. 10 somethingWith() in progress, in the spirit of make -j 10), the complexity explodes, so a mature implementation must exist somewhere.</p></li>
  <li><p>How exactly do I chain it? My take is to use a helper function which calls itself from done():</p></li>
  </ol>
  
  <hr>
  
  <pre><code>function forEachKey(obj, doSomething) {
      const q = Q.defer()
      const keys = Object.keys(obj)
      const idx = 0
      const helper = () =&gt;
         if (idx &lt; keys.length) {
             doSomething(idx).done(helper)
             idx += 1
         }
         else
         {
            q.resolve()
         }
  }
  </code></pre>