News for hackers. Stuff that matters.
Re:Asynchronous Programming in C# by mrkeen on Tuesday April 30, 2024 @07:30AM (#40209837)
> Asynchrony is viral

> 1) Once you go async, all of your callers SHOULD be async, since efforts to be async amount to nothing unless the entire call stack is async.

> 2) In many cases, being partially asynchronous can be worse than being entirely synchronous.

> 3) Therefore it is best to go all in, and make everything async at once.

I agree that all callers should become async. But I disagree that all callees should also be async, which is what 2 and 3 seem to imply.

You don't async for async's sake; you async to route around delays caused by blocking calls.

In their good example,

  public async Task<int> DoSomethingAsync()
  {
      var result = await CallDependencyAsync();
      return result + 1;
  }
they've included a line of business logic after the async machinery. If this logic grew to more than just (result + 1), I'd expect that code to be in a separate synchronous function.

I probably also wouldn't write out DoSomethingAsync() in full, either in the simple example above, or in a version with more complicated result-handling. Most of it is boilerplate: start a function call one function un-async the result into one variable do one thing to that variable repack it as async return it

If you want 'one bigger than CallDependencyAsync()', why not:

  CallDependencyAsync().ContinueWith(r => r + 1)
and if the logic grows, then:

  CallDependencyAsync().ContinueWith(r => handle(r))

  private int handle(int result) {..}
Re:Asynchronous Programming in C# by Hawxy on Tuesday April 30, 2024 @07:45AM (#40209926)
> why not

Because ContinueWith is considered antiquated in modern C# and shouldn't be used for a multitude of reasons? https://medium.com/@joni2nja/why-you-should-not-use-continue...

Re:Asynchronous Programming in C# by mrkeen on Tuesday April 30, 2024 @07:47AM (#40209949)
So does C# not have map yet?

(I figured `ContinueWith` was just dumb naming to avoid 'map' at all costs, just like how Java went with `thenApply`.)

Re:Asynchronous Programming in C# by Hawxy on Tuesday April 30, 2024 @08:39AM (#40210393)
Sure, you can use it like that, but it's not considered good practice. I think the context missing here is that most modern C# applications are going to be async all of the way down. The entry point can be async. Background service abstractions are async. Your API endpoints in ASP.NET Core are typically async as you're going to be calling some async I/O anyways (database queries, HTTP clients). There's no benefit to writing something in the old callback model when the rest of your code is async/await anyways.
Re:Asynchronous Programming in C# by pjc50 on Tuesday April 30, 2024 @08:45AM (#40210435)
> So does C# not have map yet?

Have you heard the word of our lord and savior, LINQ?

Re:Asynchronous Programming in C# by mrkeen on Tuesday April 30, 2024 @12:33PM (#40212903)
Of course.

In the article linked above: > ContinueWith allocates more than using async await

Does LINQ not carry a similar overhead?

Re:Asynchronous Programming in C# by pjc50 on Wednesday May 01, 2024 @04:12AM (#40220758)
You said, "So does C# not have map yet?", knowing full well that it did, and are now moving the goalposts.
Re:Asynchronous Programming in C# by mrkeen on Wednesday May 01, 2024 @07:45AM (#40221949)
I'll rephrase.

Does LINQ's map come with a plethora of problems like I'm told Task's map does above?

Should you avoid mapping because 'it's the old callback model' which doesn't belong in modern code?

Is map fit for purpose, or has it been disavowed?

Re:Asynchronous Programming in C# by pjc50 on Wednesday May 01, 2024 @09:19AM (#40222791)
Mapping (spelt "Select" quite often in LINQ) is entirely normal and promoted going forwards. It can optimize extremely well for simple cases. By default it is not parallel and compiles to an underlying for loop.

Or you can use it with Tasks. https://itnext.io/fun-csharp-doing-linq-to-tasks-because-tas...

(caution: contains the M word)

More from Microsoft: https://devblogs.microsoft.com/pfxteam/tasks-monads-and-linq... ; referencing Lippert https://ericlippert.com/category/monads/

ContinueWith is not a broken "map". What people usually want is Select. https://learn.microsoft.com/en-us/dotnet/csharp/linq/standar...

Re:Asynchronous Programming in C# by Salgat on Tuesday April 30, 2024 @07:34AM (#40209857)
They're referring to not using .Wait()/.Result on awaitable functions, not about making every function async regardless of whether it needs it. This was a common pitfall that people would do back in the day to avoid having to refactor everything when adding async support to the code, since one await deep in the code could cause half your functions to need async added in a refractor.
Re:Asynchronous Programming in C# by mrkeen on Tuesday April 30, 2024 @07:41AM (#40209896)
> not about making every function async regardless of whether it needs it.

That's what you and I wrote. That's not what the article wrote.

"efforts to be async amount to nothing unless the entire call stack is async"

The entire call stack?

"it is best to go all in, and make everything async at once."

Go all in?

This beyond 'a little bit unclear' into 'incorrect' territory, especially since the preceding paragraph said "there's been lots of confusion on the best practices for async and how to use it properly."

Is there a single NB or asterisk-like comment on the whole page that walks back the "all in" comment to something like "calling sync from async is fine." ?

Re:Asynchronous Programming in C# by Salgat on Tuesday April 30, 2024 @08:15AM (#40210180)
He explains the same thing I did right in his example. Don't use .Result to force sync over async.
Re:Asynchronous Programming in C# by mrkeen on Tuesday April 30, 2024 @08:25AM (#40210286)
He says not to blocking-wait for async methods, which is correct.

He makes no direct comment, afaik, on calling sync methods from async methods. The reader is left only with

  efforts to be async amount to nothing unless the entire call stack is async
and

  it is best to go all in, and make everything async at once.
and must somehow parse this as some of the call stack, and make some things async.
Re:Asynchronous Programming in C# by davidfowl on Thursday May 02, 2024 @02:26AM (#40233274)
If I made mistakes feel free to file an issue or even send me a PR. It's open source! That said, you're right that I don't say how best to call sync from async methods (the latter is more difficult as there's no good way do it well).