Created
February 4, 2021 16:52
-
-
Save davidfowl/c72f0973919e0e18708a5c345a2a4778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var t1 = DoFooAsync(obj); | |
var t2 = DoBarAsync(obj); | |
var t = await WhenAnySuccessOrAllFail(t1, t2); | |
async Task WhenAnySuccessOrAllFail(params Task[] tasks) | |
{ | |
var remaining = new List<Task>(tasks); | |
while (remaining.Count > 0) | |
{ | |
var t = await Task.WhenAny(remaining); | |
if (t.IsCompletedSuccessfully) | |
{ | |
// Successful so return it | |
return t; | |
} | |
// It failed, remove it from the remaining tasks to process | |
remaining.Remove(t); | |
} | |
// Both failed (will bubble an aggregate exception) | |
return await Task.WhenAll(tasks); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
F# code I ended up with: