igritco
1/3/2019 - 6:16 PM

Async in constructor

When you need to call an async method in the constructor, you'll not be able to do it with await, so you'll leave it as it is. The problem is that in case you'll have an exception there, you'll never know - it'll not be rethrown. In order to fix this use ICommand as follow:

ctor() { DoSomething(); }

private ICommand DoSomething => dosomething??dosomething=new Command(async()=> await DoSomethingAsync);

ctor()
{
DoSomething();
}

private ICommand DoSomething => dosomething??dosomething=new Command(async()=> await DoSomethingAsync);