Introducing Promises
From the previous considerations, we can conclude that, despite their common usage, callback functions are not so suitable to manage asynchronous programming. They allow us to execute asynchronous code, but we have not a strong control on synchronization, error handling, and code readability.
In last years, an alternative pattern for managing asynchronous code is spreading in the JavaScript community—the Promise pattern.
What are Promises?
Promises are objects that represent a value that we can handle at some point in the future. They can be used to capture the outcome of an asynchronous activity, such as an event, and to manage it in a consistent way. In fact, unlike event handling, using callbacks, Promises guarantees us to receive a result, even if the event occurs before we register to handle it (in contrast to event that can incur in race conditions) and allow us to catch and handle exceptions. Moreover, they allow us to write code with a synchronous style gaining...