JavaScript ES6 promise for loop
<pre><code>for (let i = 0; i &lt; 10; i++) {
    const promise = new Promise((resolve, reject) =&gt; {
        const timeout = Math.random() * 1000;
        setTimeout(() =&gt; {
            console.log(i);
        }, timeout);
    });

    // TODO: Chain this promise to the previous one (maybe without having it running?)
}
</code></pre>

<p>The above will give the following random output:</p>

<pre><code>6
9
4
8
5
1
7
2
3
0
</code></pre>

<p>The task is simple: Make sure each promise runs only after the other one (<code>.then()</code>).</p>

<p>For some reason, I couldn't find a way to do it.</p>

<p>I tried generator functions (<code>yield</code>), tried simple functions that return a promise, but at the end of the day it always comes down to the same problem: <strong>The loop is synchronous</strong>.</p>

<p>With <a href="https://caolan.github.io/async/" rel="noreferrer">async</a> I'd simply use <code>async.series()</code>.</p>

<p>How do you solve it?</p>