Sharing Top Content from the Angular-sphere.

Using JavaScript Promises for Non-Async Control Flow?

  • I love functional languages like Elixir + Elm that give you a pipe syntax for control flow: – – It’s easy to understand what this program is doing!
  • I’ve been learning about how promises work in JavaScript, and they seem like a good way to mimic this kind of data-chaining, even if your functions aren’t really doing anything that needs to be asynchronous: – – The MDN docs on Control flow and error handling suggest that promises are…
  • Is it normal to use promises merely as a mechanism for control flow and organization?

From the DEV community. Sharing ideas that makes us all better developers.

It’s easy to understand what this program is doing! I’ve been learning about how promises work in JavaScript, and they seem like a good way to mimic this kind of data-chaining, even if your functions aren’t really doing anything that needs to be asynchronous:

The MDN docs on Control flow and error handling suggest that promises are mostly for async/deferred operations. Is it normal to use promises merely as a mechanism for control flow and organization? Is it a horrible idea for some reason?

As they mention, it is for async / deferred operations so not a good use case for synchronous operations. Good news though, the pipeline operator was proposed and is moving along quite well. If you want to read about it, @kayis has an article that talks about it.

In the meantime while we wait for the pipeline operator, you could compose your functions. If you’re new to composition, @kyleshevlin offers a short but very clear and informative explanation in his egghead.io video, Build Complex Functions with Function Composition in JavaScript.

That’s good news that JS is getting a pipeline operator! I’m occasional enough with my JS coding that I prefer to skip using build environments like Babel if I can, but it’ll be awesome when that feature gets wider browser support.

I haven’t watched the video, but in DuckDuckGo’ing JS composition of course it makes sense – you just manually curry your functions. I’ve been spoiled…

Using JavaScript Promises for Non-Async Control Flow?