r/functionalprogramming • u/Malatest • Sep 07 '23
Question use of async await vs Promise when trying to do functional javascript
Promises vs Async Await
Had an argument about my async code with my teammate, he disliked with passion my functions writen with promise chaining. I tried to explain my position by saying staff like it's more composable, error handling is nicer, such code is not in imperative style... But all with no avail. So I desided to google people opinions and like 95% of blog posts says big No to promise chaining.
Am I wrong here? And if not why majority does not think so.
8
Upvotes
5
u/iams3b Sep 07 '23
Are you doing small ops in the promise chains, or do you have a long chain where you're wrapping and creating new objects just so you can return results from nested promises, i.e.
Unless you're processing a single data structure and it all fits in together nicely, you can end up writing a lot of excessive code just to wrap and unwrap promises. It's much easier and cleaner to just do it in the async context
Be careful trying to lean too much into one pattern or another. Use the good parts of FP when you can, and lean into the syntax sugar that the language you use provides.
Lastly, when working in a team your team's style guide should always have precedence over your own wants
4
u/lingdocs Sep 07 '23 edited Sep 07 '23
Promises work a lot like monads. Promise chaining is a lot like the bind operator in Haskell (>>=). Async/await is a lot like "do" notation in Haskell. In many cases the "do" notation is preferred because it's a lot cleaner and clearer. And it's still a very Haskell/FP way of doing things. So if you think of the async/await as "do" notation for a monad, it's not imperative style at all.
TLDR; Promises are (like) monads:
Both are idiomatic in FP, both have their uses. Often people reach for #2 because it's a lot clearer and cleaner.
https://adueck.github.io/blog/functors-applicatives-and-monads-with-pictures-in-typescript/
https://haskell.mooc.fi/part2#monads-in-other-languages
4
u/jceb Sep 07 '23
I try to avoid async await for functional JS as much as I can since error handling becomes a nightmare. I would need to create lots of small try catch statements for fine-grained error handling or one big statement that makes it difficult to know where exactly things failed.
Sanctuary.js and Fluture.js are my go-to functional programming libraries. Maybe they're helpful to you as well.
4
u/KyleG Sep 07 '23
I hate try/catch blocks with a passion, but
Promiseis shitty because the type isPromise<A>, which tells you nothing about the rejection type.I personally prefer to use an asynchronous
Either-type data structure that captures the types for both success and failure cases, and then I compose them with mapping and flatmapping operations. No try/catch needed.6
u/UliKunkel1953 Sep 07 '23
Async/await lends to writing code that's simpler and easier to understand, while being sufficiently powerful. It encourages code that's optimized for reading, not necessarily quicker to write.
It sure feels great to write a dense and succinct chain of promises, but trying to understand that later can be a real problem. Even when reading your own code, let alone a coworker's.
1
u/multie9001 Sep 07 '23
My take is:
You can create an error object that has all needed parameters and data. With the error throw is just a text that references the section of code or has a message for the user. You can await anything and catch everything at the same time.
But for arrays nesting is far better than creating a variable for each step sort().filter().map()