Member-only story
Javascript Promisesâ Is There a Better Approach?
A quick look into Task, TaskEither, and Futures. Learn when to use them
A promise is an object returned by an asynchronous function, which represents the current state of the operation. At the time the promise is returned to the caller, the operation often isnât finished, but the promise object provides methods to handle the eventual success or failure of the operation. â MDN Web Docs
We use promises every day in JavaScript and its great⦠but they have their issues. On the good side, they are non-blocking and thus, fast. However, dealing with them can be cumbersome.
We often have to wait for a function to complete to get its âcallbackâ and proceed to the next task. There are no guarantees or control flow for when a promise will complete.
Additionally, we are working with unknown error types, a high likelihood of unchecked exceptions, and more. Promises co-mingle rejected promises and unintended runtime exceptions. But thankfully, there are alternatives!
In order to understand promises and their alternatives, we need to understand the different types of programming:
- Functional programming is when we build software by composing functions while avoiding shared states, mutable data, and sideâ¦