Earlier quoted context omitted.
JavaScript/TypeScript and C++ do implement a similar paradigm, loosely patterned on what C# does?
Python and Rust as well. C# invented a large part of the design of async/await that has been adopted by other languages. I figure if there's a better way, it would have been improved on by now since those other languages had plenty of time to see the issues with C#'s version.
Asynchronous Programming in C#
131–140 of 186 posts
Re: Asynchronous Programming in C#
#132Earlier quoted context omitted.
In short, they provide clean API for checking on completion and error states of a long-running process from another thread without blocking that thread. This is an essential pattern for UI-related tasks, including web pages.
Fair enough, I think we are thinking of futures slightly differently. I was thinking primarily about the deferred action to get a result (in which case channels and goroutines are equivalent with a select to handle, perhaps, an error result). You're also thinking of the other capabilities around task management and monitoring which I was not.
a := getFoo()
b := getBar()
c := getBaz()
If you want to do all 3 in parallel, you may need to use channels and wait groups and stuff. In a language with promises: const a = getFoo();
const b = getBar();
const c = getBaz();
await a
await b
await c
or even better const [a,b,c] = await Promise.all([getFoo(), getBar(), getBaz()])
If/when go generics become available, I expect to see some libraries that make things easier in golang.Re: Asynchronous Programming in C#
#133Why do we need the `async` keyword? What is the difference between a function which returns a Task , and an async function which returns a Task ?
Re: Asynchronous Programming in C#
#134I would call myself an extremely experienced and knowledgable C# programmer with 10+ years of experience and even I found a few things surprising or new in this guide. I think C# async/await implementation is the biggest con on the .NET community, because it gets constantly hailed as one of the easiest ways of async programming but this guide itself proves to me that there are so many gotchas which are not obvious at…
Re: Asynchronous Programming in C#
#135It's interesting, therefore, to try to understand why dotnet went with the async/await model.
A language maintainer C# talks about the issue here and references the rust justification for the same:
https://mail.mozilla.org/pipermail/rust-dev/2013-November/00...
https://github.com/dotnet/runtime/issues/11084
They knowingly seem to have chosen a more complicated model for performance. For me, that sounds like a bad trade. Developer time is quite a bit more valuable than compute time. The performance difference just doesn't seem to justify it.
Re: Asynchronous Programming in C#
#136Re: Asynchronous Programming in C#
#137Earlier quoted context omitted.
The way I put it: .NET async makes the easy things easier and the hard things harder. The problem is that Task/Task was the foundation for async, and it's a bad foundation. Even with the ability to write your own duck-typed awaiters (and the advent of ValueTask), the widespread use of Task means if you're writing async code you're going to have a tough time getting away from it.
I think this is the crux of the matter. Since Task and the TPL predated async, iirc, people get befuddled by the parallelism Vs concurrency (if that's the correct term) parts of the Task API. Certainly the async story is a lot more complicated in desktop but it is very simple for most server scenarios, simply put "use this async call so that the thread can do other things while you wait for the db to respond" and the…
Microsoft in general has a tendency to bolt on functionality in a kinda slapdash manner when another team wants it, so you get a lot of cruft that really doesn't belong in the Task class[1] but is there because someone wanted a way to handle their special case so it just got thrown into Task.
[1] See https://source.dot.net/#System.Private.CoreLib/Task.cs,045a7...
Re: Asynchronous Programming in C#
#138Can these be added as warnings to the compiler? Can you have custom lint/compiler warnings from the community like eslint?
Re: Asynchronous Programming in C#
#139Re: Asynchronous Programming in C#
#140async/await makes things complicated - this is a great illustration of some issues with it. Fibers/green threads/goroutines seem to be generally easier and not viral. C#/dotnet choosing this the async model always bothered me. Otherwise, the platform is solid. It's interesting, therefore, to try to understand why dotnet went with the async/await model. A language maintainer C# talks about the issue here and reference…
For Rust, one of the feature of Rust is to have a minimal runtime, so using a compiler transformation seems a good fit.
For C#, Microsoft has a limited number of people working on the runtime of DotNet. Async/await was developed at the same time DotNet was transitioning to DotNet Core which also requires massive engineering, so it was about priority. The future will tell if at some point coroutine will be added to DotNet.