Comment

Comment by mrkeen

> 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) {..}
parent mrkeen 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton