- Mastering JavaScript Promises
- Muzzamil Hussain
- 521字
- 2021-07-16 13:46:51
Promises and exceptions
Consider a function that throws exceptions within the promise paradigm. You won't find any trace or log if you try to see what has happened to the exception-throwing function. You will see no output on the screen or on console. Why? The reason is hidden in the basics of promise.
Promises are designed to produce two types of output—a promise will either be fulfilled or rejected. So naturally, it won't show up anywhere at the output streams since promises are not designed to produce any other output except these two predefined states. However, it's not promise that does not give any facility to handle exceptions. In fact, it provides a robust way to show and handle such exceptions, by implementing a proper handler to catch the exception and display the reason at any desirable output stream.
In most of the promises paradigm, the exception is handled by fail
and then
. The handlers differ from one library to another and from one language to another language. In many advance high-level languages, error and exception handling are managed automatically without adding much of code and explicitly telling compiler/ interpreter, but in those libraries and languages, in which it's not handled on auto-basis, you have to write an explicit code to handle exception manually.
At this point, it's significant to mention that we are using a bit of code from Q, just to make you understand that exemption handling is also a part of promises implementation and how to deal with an exception, if one occurs. In our next chapter, we will focus more on how to implement promises in other libraries and languages.
Coming back to the topic, like many other implementations, Q has its own mechanism of dealing with promises.
Consider that this code is about to throw an exception:
function imException() { throw "imException"; }//end of code
Since it's not the right implementation of handling exception in promises using Q, there will be no output at all, and if we want to handle it as per implementation of the Promises paradigm in Q, we will want to add a rejection handler.
Let's take Q as an example, in order to see if we can add the same function using its fcall()
method:
Q.fcall(imException);
This method call is not meant to handle the exceptions, so it won't show anything. To handle it, we need to add a rejection handler that will help us to track and monitor the exception.
The fail method
The simplest way to handle exception is using fail
. Let's restructure our code to implement the fail
method:
// code view before exception handler Q.fcall(imException); //code after exception handler Q.fcall(imException) .fail(function(err) { console.log(err); });
The then method
Normally, we would use then
to handle chaining promise. This will take two arguments and the return promise-based execution of one of these handlers:
Q.fcall(imException) .then( // first handler-fulfill function() { }, // second handler -reject function(err) { console.log(err); } );
The first argument was a fulfill method and the second is rejection handler, as shown in the preceding code. Using these simple techniques, Q implements exception handling.