3 ms·
That .NET syntax using Task.WhenAll seems quite bad, which might be part of the reason why not many people bother (disclaimer: I don't do C# or ASP.NET). In Rus
by lxpz 6y ago
That .NET syntax using Task.WhenAll seems quite bad, which might be part of the reason why not many people bother (disclaimer: I don't do C# or ASP.NET). In Rust it would be:
let p, c, m = join!(
GetUserPermission(username),
GetServerConfig(),
GetMessageOftheDay()
);
(you don't even have to write await when using the join macro)
With such simple syntax available it seems obvious to me that one would want to use it as often as possible, and it's also much simpler (and probably cheaper) than dispatching those three tasks to a thread pool.
- moocowtruck 6y agovar r = await Task.Whenall(f1,f2,f3); Console.WriteLine($"{r[0]}, {r[1]}, {r[2]}"); f1,f2,f3 are all async fn's, that is all you have to do
- Serow225 6y agoyeah, the original example is showing the unwieldy version of the syntax.
- jiggawatts 6y agoThe original example by me did not assume that asynchronous functions all return the same result type. Mist opportunities for concurrency are between unrelated tasks (because related tasks often dependencies between them). Unrelated tasks tend to have unrelated return types.
- concretemarble 6y agoWhen tasks are unrelated, you also likely don't need them all at the same time for the next stage of pipeline. You can simply await it when you need it. var p_t = GetUserPermission(username); var c_t = GetServerConfig(); var m_t = GetMessageOfTheDay(); function_to_call1(await p_t, await c_t); function_to_call2(await m_t); This does not look any more complicated than a non-async function. Not sure how this example justifies your claims. Besides, even if your example is valid, the usage of Task.WhenAll has nothing to do with your claims either. The use of async/await is majorly for scalability. Being able to make several network calls concurrently is not the major concern. Even if you await at each async call, you still achieve better scalability because threads won't be blocked for async calls and can work on something else.