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#.
Well, there are various options:
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)
{
// ...
}
See more on this question at Stackoverflow