[MUSIC]
When we were talking about services in the previous module, I had mentioned
about promises and then I promised that I will return to promises in this module.
So, here we are I'm going to fulfill my promise to you.
A synchronous computation is all over the place in the world wide web.
And how the web operates.
If you are coming from the sequential programming world,
wrapping your head around a synchronous computation takes quite a bit of effort.
There have been many attempts at making asynchronous computation as painless as
possible for people transitioning into it from the sequential computation book.
One such attempt are to making asynchronous computation
easier to accomplish is promises.
As we realize, when we make any request to any
service or to a backend server,
it is going to take some time for the results to become available for us.
If we do such a request while we are proceeding with our work,
then there is a high likelihood that our computation will get stuck at the point,
waiting for the results.
This is a no-no in terms of doing a good user experience.
So it is important that whenever your results are going to take time
you should be able to proceed forward without waiting for the rules.
And that is where asynchronous computation comes to your advantage.
In asynchronous computation when you send out a request
you are expecting the results to come back at a later time, and
you should be able to deal with the fact, the base results come back at a time
much later than where you were at the point when you made the request.
Promises provide us with a way of dealing with this.
Let's look at promises in a little more detail in this lecture.
And then we will also see the use of promises
in our Angular application a little bit later.
Promises, the general concept is a very vast area by itself.
And in this course, I don't have time to go through the details of promises.
That'll become an entire course in itself.
I will give you just the essentials of promises that are necessary for
us to understand how to make use of promises within our angular application.
I do provide you with a lot of leads to additional
resources where you can go and examine promises in more detail.
So coming down to that question, what exactly is a promise?
A promise is a mechanism that supports asynchronous computation.
I just gave a long winded explanation of why asynchronous computation
is essential for the web to function.
Now, in using a promise, the promise provides a mechanism or
a proxy for a value that is not available at the moment that you ask for it.
So a promise will return a promise to you, saying that
when the result becomes available, it'll come back and deliver the results to you.
So a promise returns a proxy object to you, and you hold on to the proxy object.
And that proxy object will give you access to the results when they become available.
And there is a method in how you wait for the results.
You actually don't suspend your computation at that point instead you
configure a way of accepting the results when it comes back from that promise.
To put it in a nutshell,
promise represents a value that will be available at a future time.
It may be also immediately available,
in which case the promise will return the value immediately.
But in case it needs to return a value at a future date, then it gives you
the promise that that value when it becomes available will be returned to you.
There is always the possibility that the promise cannot be met,
because the results are unavailable for whatever reason.
In that case, an error will be delivered to you, or a reject will be delivered
to you, and it is up to you to deal with the fact that the promise was rejected.
We look at how we deal with that in the next slide.
As we learned earlier, a promise is a proxy for
something that was going to happen in the future or may never happen in the future.
So a promise when it is given to you, will be in the pending state.
Now when the promise resolves, that is the results become available and
the promise is able to deliver the results to you, then the promise will resolve or
will fulfill.
And in that case the promise will move from the pending state to the resolve or
the fulfill state.
So at that point your results are available to you.
There is a slight chance that the promise may be rejected because of an error,
or whatever be the reason.
In that remote circumstance,
you will be delivered a reject and in that case, the promise will be
considered to be in the rejected state when the reject is delivered.
So, when you generate a promise, as I said,
a promise is a proxy object that is delivered to you.
So, when you deliver a promise to somebody, the promise internally will have
a function that will deal with both the resolve and
the reject options inside there.
When you write code,
you will see that you'll write code as new Promise ( function (resolve, reject).
So whether it resolves or it gets rejected, we need to be able to send
back the information to whoever we delivered the promise to.
How do we do this in code?
Let's take a look at that in the next slide.
Here is an example of how promise is configured in code.
Indeed, this example is derived from the exercise that you will do
a little bit later.
So here, when we configure our service, we can have our service
return a promise to a component that requests for data from the service.
So when the promise is returned,
then the return promise is available to you as an object.
And this promise provides two methods, the then and the catch method.
Now, then, the promise is successfully resolved.
Then the promise will result
in calling a resolve.
As you saw, the promise is constructed, and inside the promise,
you have the function, the resolve, and reject, as the two parameters.
So if the promise is successfully able to deliver the result,
then it'll call the resolve method and then return the result to you.
Where does the result get reality?
When you write code waiting for
the promise then you would specify what to do when the promise returns the result
in the then option for that promise.
So say for example, you would say getDishes.then and inside of then
you would supply the function that will deal with the result when it comes in.
If the promise is not able to resolve correctly then it'll reject
with the error being sent back to you.
The error being an error object that'll be sent back to you.
So when their promise get rejected, then you deal with
the rejection using a catch within your code.
So within the catch, you will provide a function that will deal with the error.
For example, you may simply output the error message,
the console log and deal with the error that way.
Or you might have a more elaborate way of handling the error.
That would be provided in the catch option that you give for that promise code
on your side when the promise delivers a reject to you.
So this is how the two sides of the coin deal with promises.
From one side, you are sent a promise object, and
when you receive the promise object, with the promise object, you would either do
the then and the catch, so you're supposed to implement both the then and
the catch metrics changed to that promise object.
And then, from that promise aside, if the promise results correctly,
then the resolve will be called and thereby, the result will be delivered
in the then method of your promise.
If the promise gets rejected,
the reject call will deliver the error into your catch.
So this is how you would implement code using promises.
We will deal with the example in the exercise that follows this lecture.
Now obviously the question I ask is why promises?
Later on, when we deal with Node JS,
we will deal with how we can construct callbacks.
And I will explain the details of callbacks and the way to deal with
callbacks when we cover the Node JS in the last course of this specialization.
Callbacks have a problem of creating what to is referred to as callback hell.
So you have heavily nested callback code
that is very complicated to be able to figure out what it is trying to do.
Promises provide a way of addressing this callback hell problem.
But for the moment we are concentrating on Angular, I'll just mention this point.
And then we'll come back to deal with this question in the last course.
The other advantage of using promises is that promises can be changed.
A promise when it delivers the result in the bin,
you can generate another promise and return, and
that promise can be dealt with by chaining another then method to that and so on.
So you can chain a set of then methods together to deal with
promises one behind other.
If the result is immediately available or if you are going to reject immediately,
then promise provides a method of immediately returning without waiting for
the promise to either resolve or reject.
In that case, you will simply say promise with a capital P.resolve and
Promise.reject and then return either the result or the error immediately.
In the first exercise, we will deal with this method of returning the value
from our service to our components.
In the second part of exercise, we will deal with
the situation where the promise will resolve after a time duration.
And also within our component, we should be able to deal with the fact that
the results come back a little bit later than when you ask for the result.
With this quick understanding of promises,
let's go on to the next exercise where we will deal with promises.
[MUSIC]