In the previous exercise,
we learned about two new aspects of JavaScript,
their support for first class functions and closures.
We also saw how Node handles the asynchronous programming aspect and how Node
supports asynchronous execution of code with
the use of callbacks and also error handling.
Let's look at an example of how we make use of this
within our Node application in this exercise.
Continuing with where we left off in the previous exercise,
now I'm going to go and modify the rectangle Node module,
so that it can take the help of callbacks.
So I'm going to remove these exports here,
and instead, they're going to modify this function.
I'm going to be making use of these two functions a little bit later,
so that's why I'm saving them at the bottom.
So let me keep them aside.
And then I will modify this rectangle module as follows.
So we'll say module.exports.
So we're are using the standard way the module is handled.
And this is going to export this function which takes these three parameters,
x and y and callback.
The callback is a function that is going to be supplied in when this module is called.
So this is the use of the first class functions that we have talked about earlier.
So inside this callback,
so you can see that you're getting two parameters, x and y.
In this case, for the rectangle,
the x and y correspond to the length and breadth of
the rectangle that is being passed in as the two values.
So inside here, we're going to check to see
if x is less than or equal to zero or y is less than or equal to zero.
We have written code like this here.
So I'm just going to copy this code from here
and then bring it over to here and then we'll edit.
So in this case, this is if x is less
than or equal to zero or y is less than or equal to zero.
In this case, what do we do?
In this case, we notice that
the dimensions of the rectangle are less than or equal to zero.
So we have to handle it differently,
and we'll do the else part of it.
The else part where we handle the situation where the rectangle is a valid rectangle.
So in this case,
what we will do is to then use a setTimeout.
So here, I am simulating the fact that whatever
is being done in this rectangle is going to take some time.
So this is the asynchronous processing.
Now, since I don't have a lot of work to do in the background,
so I'm going to simulate that by simply using
the setTimeout function of JavaScript and then
delay before the callback function is called.
So the way I arrange this is as follows.
So inside the setTimeout,
I'm going to call this function there
and arrange a delay of
2,000 milliseconds or two seconds after which this function is going to be called.
So if you recall,
the setTimeout that is supported in JavaScript,
it takes as the first parameter a function and
the second parameter would be the time period for which this is going to be delayed.
So let me indent this in.
Now, I need to fill in this function here.
So I have started out with the arrow function here
which takes no parameters and then when there is called,
I'm going to issue a call to the callback.
This callback is a callback function that is
going to be passed in as the third parameter here.
This callback function, as you noticed, takes two parameters.
The first one is the error and the second one is the return value.
So in this case, since we have an error,
because x is less than or equal to zero and y is less than or equal to zero,
so the first value I'm going to pass in as new error,
and this error is exactly this string that I was using in the console.log then,
and that is what I will pass in as the error here.
Let me now delete that console.log,
I no longer need it.
So for the callback,
I'm going to generate a new error object and then pass this
in as the return value for the callback function, the first parameter.
So I'll say rectangle dimensions should be greater than zero.
So we'll say l,
this is the x and the y value that we got as the input values there.
So that is the callback.
And the second part of this callback,
I'm going to pass in as null because this is going to return an error.
So when you return an error as the first parameter,
the second parameter will be ignored when that callback was just received by
the node module from where we are calling this particular function there.
So, with this arrangement,
so less than zero,
so let me give some space here so that it's properly indented here.
So, the way I am arranging this
here is that if x is less than zero and y is less than zero,
I'm going to callback the callback function that has been passed in,
but the first parameter will pass in in
error because here we notice that there is an error,
and the second parameter will be null.
If this is not the case,
then I'm going to call the same callback,
but with the first parameter.
So let me copy that code in here.
I'm still going to use the setTimeout here.
But that first part is not going to be an error,
instead, the first part,
in this case, there is no error.
So I'm just going to pass back that value as null.
So that means that the error is set to null.
So this is a valid rectangle.
So we can compute the values for the rectangle.
But instead, I'm going to simply pass in
a JavaScript object containing the two functions
as the two values inside this JavaScript object.
So, here I am going to take this two,
the perimeter and the area,