C# web scaffolding has an async call for every member. Why?
For example: Async for a login call makes no sense. User has to wait for validation. What else would the app do until the application validated the user.
Thoughts?
-
Possibly the user may want to cancel the login process? If done synchronously then the app cannot take further input until the login request has been handled to its conclusion. It may be an unlikely event (unless the login request can potentially take a significant period of time to return) but then the asynchrony may be used just for programming consistency rather than functionality.Steve– Steve2019年05月28日 21:43:20 +00:00Commented May 28, 2019 at 21:43
-
For web application, client have nothing to do with server side async-await. Response will be returned only when method(login) will be completeFabio– Fabio2019年05月28日 23:48:31 +00:00Commented May 28, 2019 at 23:48
-
"What else would the app do until the application validated the user." Serve a request from another user. If you only have one user, then a web app may not be your best solution. If you have multiple users, they don't want to have to wait whilst the server completes a synchronous handling of just one request before moving on to the next. So everything is asynchronous.David Arno– David Arno2019年05月29日 12:25:42 +00:00Commented May 29, 2019 at 12:25
1 Answer 1
The purpose of async
is not to return a value immediately; it is to allow your code to work on something else while your method await
s a return value.
async
doesn't magically make your method run faster. It still has to compute a result. What async
does do is make your code non-blocking on the server, and it can often do it without spinning up additional threads, because it merely reorders your code while respecting the order of return values.
To find out more about how this process works, read this MSDN article.
-
By the way, doesn't
async
, by itswait for this value
nature impose, when working with interfaces, an animation or message about the fact that it's working? I found that designing around it often makes me think a lot more about my interface than I should. Is this an innate thing toasync
or a byproduct of good standards?coolpasta– coolpasta2019年05月29日 01:12:02 +00:00Commented May 29, 2019 at 1:12 -
@coolpasta: The
Task<T>
interface thatasync
works with has aStatus
member that includesIsCanceled
,IsCompleted
andIsFaulted
properties.Robert Harvey– Robert Harvey2019年05月29日 02:30:58 +00:00Commented May 29, 2019 at 2:30