Detecting and treating errors in asynchronous code are hard tasks. For synchronous code, we can use the famous try/catch/finally block. Treating an exception with this block is easy, as can be seen in the following code:
try{
throw new Error('An error occurred');
}catch(err){
console.log('Treating the error');
}finally{
console.log('Last computation');
}
Unfortunately, we can't use the try/catch/finally on asynchronous code. In JavaScript we run asynchronous code most of the time, so the language and the community elaborated a couple of strategies.
Initially, the only strategy we had available to deal with asynchronous code was the use of callback functions, so most libraries in JavaScript added the onSuccess() and onError() methods, which accepted a callback function to be executed when an error happened or when data is available. Different frameworks...