Is it possible to use asynchronous methods in a chain?
var blog = new Blog(); await blog.ValidateAsync().InsertAsync().SaveAsync(); Is it possible to use asynchronous methods in a chain?
var blog = new Blog(); await blog.ValidateAsync().InsertAsync().SaveAsync(); Not. All asynchronous methods return Task , and to get to the result of the task, you need to add the await keyword. Since the second method you need to call the result of the first task, you must first wait for this task. And so on. The result is the following nested chain:
await (await (await blog.ValidateAsync()).InsertAsync()).SaveAsync(); You can arrange this with crutches using extension methods, where InsertAsync() will be an extension method for some Task<T> , where T contains everything necessary for the method to work. Similarly with SaveAsync() . And the rest of the expectations will occur within these methods.
However, in your case I would not advise doing this. Fluent syntax is good in very specific scenarios, and the "validation - insert - save" chain, in my opinion, is not included in them. It is best to call these methods consistently, not to save on lines and not to strive for "beauty."
In such a chain - no, it is impossible .
You can write except that:
await (await (await blog.ValidateAsync()).InsertAsync()).SaveAsync(); But it is better to clearly separate the different operations:
await blog.ValidateAsync(); await blog.InsertAsync(); await blog.SaveAsync(); Source: https://ru.stackoverflow.com/questions/721415/
All Articles