Timeout in async/await
<p>I'm with Node.js and TypeScript and I'm using <code>async/await</code>.
This is my test case:</p>

<pre><code>async function doSomethingInSeries() {
    const res1 = await callApi();
    const res2 = await persistInDB(res1);
    const res3 = await doHeavyComputation(res1);
    return 'simle';
}
</code></pre>

<p>I'd like to set a timeout for the overall function. I.e. if <code>res1</code> takes 2 seconds, <code>res2</code> takes 0.5 seconds, <code>res3</code> takes 5 seconds I'd like to have a timeout that after 3 seconds let me throw an error.</p>

<p>With a normal <code>setTimeout</code> call is a problem because the scope is lost:</p>

<pre><code>async function doSomethingInSeries() {
    const timerId = setTimeout(function() {
        throw new Error('timeout');
    });

    const res1 = await callApi();
    const res2 = await persistInDB(res1);
    const res3 = await doHeavyComputation(res1);

    clearTimeout(timerId);

    return 'simle';
}
</code></pre>

<p>And I cannot catch it with normal <code>Promise.catch</code>:</p>

<pre><code>doSomethingInSeries().catch(function(err) {
    // errors in res1, res2, res3 will be catched here
    // but the setTimeout thing is not!!
});
</code></pre>

<p>Any ideas on how to resolve?</p>