Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Proper way to call async method from component property

$
0
0

When consuming a component in another razor component, there is usually a need to call async methods triggered by an EventCallback property, onClick for instance. Is there any particular way to call it to assure asynchronicity?

Let's assume the following example:

@* CASE 1*@<MyButton OnClick=DoSomething>Click Me</MyButton>@* CASE 2*@<MyButton OnClick=@(async () => await DoSomething())>Click Me</MyButton>@code {    private async Task DoSomething(){ ... }}

After compiling, is there any distinction between using a delegate or calling the function directly? Should we prefer one or the other?

This question doesn't derive from an error or code running improperly but only from the fact that I get no feedback from Visual Studio and probably for a good reason but I would like to know if I'm writing improper code either way.

Choice Remark: Hard to pick a single answer for all provide valid points to the discussion, further reading is encouraged. It seems the confusion stems from a misunderstanding of the delegate's role in the call stack, in that way I think that Shaun's answer shows the most succinct and explicit example of that.


Viewing all articles
Browse latest Browse all 4839

Trending Articles