Skip the await task response conditional

Essentially what you want is to cancel a task, but with a little more logic.

You need to edit doSomethingElse so that it accepts a CancellationToken, and also so that it makes use of it to stop what its doing:

public async Task<Foo> DoSomethingElse(CancellationToken token) {
    ...
    if (token.IsCancellationRequested) {
        // stop what you are doing...
        // I can't tell you how to implement this without seeing how DoSomethingElse is implemented
    }
    ...
}

Now, get a CancellationToken from a CancellationTokenSource:

var source = new CancellationTokenSource();
var token = source.Token;

And here comes the logic of "if response 1 fails cancel response 2":

var response2Task = DoSomethingElse(token);
var response1 = await DoSomething();
if (!response1.IsSuccess) {
    source.Cancel();
} else {
    var response2 = await response2Task;
}

var task2 = doSomethingElse();
var response1 = await doSomething();

if(response1.isSuccess) {
    var response2 = await task2;
}

This will start the execution of doSomethingElse() immediately, and only wait for its completion when response1.isSuccess == true

Tags:

C#