Error callback for a Task type in C#

I was wondering whats the error callback for a Task in C#.

For example : In JavaScript you have two callbacks for a promise.

obj.save().then(function(){
   //success
}, function(){
   //error
});

Whats the concept in C# with async/await.

Task task = obj.SaveAsync();

Where is the error callback in Task?

P.S : I'm migrating from JavaScript to C#.

Jon Skeet
people
quotationmark

Well, there are various options:

  • You could use Task.ContinueWith, specifying the error callback using TaskContinuationOptions.OnlyOnFaulted
  • You could await the task and just catch the exception which will be unwrapped accordingly:

    try
    {
        await obj.SaveAsync();
    }
    catch (BadStuffHappenedException e)
    {
        // ...
    }
    

people

See more on this question at Stackoverflow