Timeout in async/await

You can use Promise.race to make a timeout: Promise.race([ doSomethingInSeries(), new Promise((_, reject) => setTimeout(() => reject(new Error(‘timeout’)), 11.5e3)) ]).catch(function(err) { // errors in res1, res2, res3 and the timeout will be caught here }) You cannot use setTimeout without wrapping it in a promise.

General purpose FromEvent method

Here you go: internal class TaskCompletionSourceHolder { private readonly TaskCompletionSource<object[]> m_tcs; internal object Target { get; set; } internal EventInfo EventInfo { get; set; } internal Delegate Delegate { get; set; } internal TaskCompletionSourceHolder(TaskCompletionSource<object[]> tsc) { m_tcs = tsc; } private void SetResult(params object[] args) { // this method will be called from emitted IL … Read more

Fire and Forget approach

It depends on the semantics you want. If you want to ensure exceptions are noticed, then yes, you could await the task. But in that case it’s not truly “fire and forget”. A true “fire and forget” – in the sense that you don’t care about when it completes or whether it completes successfully or … Read more

Why does this async action hang when I try and access the Result property of my Task?

Yep, that’s a deadlock all right. And a common mistake with the TPL, so don’t feel bad. When you write await foo, the runtime, by default, schedules the continuation of the function on the same SynchronizationContext that the method started on. In English, let’s say you called your ExecuteAsync from the UI thread. Your query … Read more

How to wait for a JavaScript Promise to resolve before resuming function?

I’m wondering if there is any way to get a value from a Promise or wait (block/sleep) until it has resolved, similar to .NET’s IAsyncResult.WaitHandle.WaitOne(). I know JavaScript is single-threaded, but I’m hoping that doesn’t mean that a function can’t yield. The current generation of Javascript in browsers does not have a wait() or sleep() … Read more

Why should I prefer single ‘await Task.WhenAll’ over multiple awaits?

Yes, use WhenAll because it propagates all errors at once. With the multiple awaits, you lose errors if one of the earlier awaits throws. Another important difference is that WhenAll will wait for all tasks to complete even in the presence of failures (faulted or canceled tasks). Awaiting manually in sequence would cause unexpected concurrency … Read more