DEV Community

Discussion on: What is Async Programming, and Why Should You Care?

Collapse
 
mileswatson profile image
Miles Watson

Are you referring to the initial Task object that the function returns? Or the result when the Task object is awaited?

Collapse
 
muttsuri profile image
Muttsuri

Given the async function you wrote. How would I work then with it's result.
I've heard some things on how Task in C# is monadic so I assume that you never leave the async state and just chain function execution kinda like the javascript's .then()

Thread Thread
 
mileswatson profile image
Miles Watson • Edited

The GetAllPages function returns a task, which can either be awaited (if called by an async function) or synchronously waited using .Result (if called by a synchronous function).

I’m not familiar with the term “monadic”, however I think you are correct that async generates some boilerplate code. You write it as if the code is a single function block, but behind the scenes it automatically generates exit and entry points before and after each await. So yes, I think it is similar to JavaScript’s .then() functionality.

Thread Thread
 
muttsuri profile image
Muttsuri

In a very naive term that will probably get me hunted by the FP Inquisition, I would risk saying that a monad is a "context box".
Examples are things like Thask where you chain tbem with await (or .then()) or sometimes you get to take the thing out of the box.
Another famous example is IO, since those operations can fail the result such a function gets wraped in IO so that it can just be called in the middle of any old function without basically forcing you to wrap the enclosing function in IO