Waiting and Asink on the same line

I understand how async works and how it compares with javascript promises, but I'm not sure how a line like the one below can have any benefit

IdentityResult result = await UserManager.CreateAsync(user, model.password);

since we create an asynchronous call and immediately wait in the thread so that subsequent lines are not executed until the call to the Async call ends.

+3
source share
2 answers

The advantage is that if this operation is truly asynchronous, then at some point the calling thread will be freed up to do other work in your application, and not be blocked synchronously.

This use provides higher scalability.

, , . , , .

, CreateAsync :

async Task<IdentityResult> CreateAsync()
{
    // do something
    await Task.Delay(1000);
    // do something else
}

await Task.Delay(1000) , . .

CreateAsync , Task.Delay

+5

, , UserManager.CreateAsync , .

CreateAsync, Async , , , async .

CreateAsync , , .

, - . CancellationToken CreateAsync, , , .

+1

All Articles