C++ future async equivalent in node.js : <p>I want to write the following C++ code equivalent in Node.js.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;future&gt;
#include &lt;string&gt;

std::mutex mut;

void print(const std::string&amp; message, int n) {
    for (int i = 0; i &lt; n; ++i) {
        {
            std::lock_guard&lt;std::mutex&gt; lk(mut);
            std::cout &lt;&lt;  message &lt;&lt; std::endl;
        }
        usleep(200000);
    }
    std::lock_guard&lt;std::mutex&gt; lk(mut);
    std::cout &lt;&lt; message &lt;&lt; " finished" &lt;&lt; std::endl;;
}

int main() {
    std::future&lt;void&gt; fut_a = std::async(print, "a", 2);
    std::future&lt;void&gt; fut_b = std::async(print, "b", 8);
    fut_a.get();
    std::future&lt;void&gt; fut_c = std::async(print, "c", 2);
    std::future&lt;void&gt; fut_d = std::async(print, "d", 2);
    fut_c.get();
    fut_d.get();
    fut_b.get();

}
</code></pre>

<p>The output is: a b a b b c d b d c b b b b</p>

<p>In Node.js v8.6.0 the following code starts displaying "c" before displaying "a" is finished.</p>

<pre><code>const asyncfunc = (message, n) =&gt; {
    for (let i = 0; i &lt; n; ++i) {
        setTimeout(() =&gt; {
            console.log(message);
        }, 200 * i);
    }
}

async function main() {
    const a = asyncfunc("a", 2);
    const b = asyncfunc("b", 8);
    await a;
    const c = asyncfunc("c", 2);
    const d = asyncfunc("d", 2);
    await c;
    await d;
    await b;
}

main();
</code></pre>

<p>The output is: a b c d a b c d b b b b b b</p>

<p>Is there any handy ways in Node.js to write the above C++ code equivalent?</p>
