In the last lecture we looked at callbacks as a way to write asynchronous code callback functions are
really really easy to implement and they're pretty simple callback functions How ever can become quite
messy and complicated to maintain as we nest functions within functions especially has the level of
nesting increases.
So there is another way to write asynchronous code and this is call as the concept of promises callback
function runs when it is triggered by the calling function.
So it in effect will wait until it is called by the calling function.
Similarly a promise is a way to wait for something to happen.
Promises provide a much cleaner way to write asynchronous code.
Let's understand this with an example.
Let's say we write function do stuff
that takes into arguments data
and a callback and it simply calls the callback function with a string say the
we can write this exact same function using promises.
So let's cut and out this code and see how to rewrite it using promises.
So we do it like a function do stuff.
And this time there is a single argument.
Data will need a callback function when using promises.
What we are going to do is return a promise a promise is something you can wait on for it to either
resolve or reject or in other words either succeed or fail.
So if there is an error during the execution of the function it could reject the promise and if it completes
successfully you could resolve the wrongs
first.
We're going to use the return keyword.
We have to return the promise.
So the caller has access to it.
So we returned a new promise.
Promise is a predefined construct that takes in our function as an argument
and this function in turn takes two arguments resolve and reject what what we are doing here is creating
a new promise and returning it to the caller.
So the caller can use it as needed.
So if everything goes well you can call resolve and if there's an error we could call reject.
So let's say a type of data
travel equal to encodes Boullion
and data table equal to
two
we resolved that promise.
Like so otherwise we reject the promise in a similar fashion.
The triple equal to operator are compared to the left hand side with the right hand side and returns
true only if the both sides have the same value as well as the same day that I you can't pass any type
of data as an argument to resolve and reject.
So you could best ratings numbers booleans or object only thing to remember here is you can't pass only
one argument.
So the best way to pass this data is by using an object using an object allows you to pass several items
or attributes of the data within a single argument.
So let's pass an object that retains a status and a message string for example.
So if everything goes fine we resolve the promise using an object that saves status
it's success and message is
all is well.
And if there is an error we reject a promise with an object
with status
as editor and message as
loops.
There was
an editor something like that
and that's come into our the reject far for now and run this function
we call the function that uses promises just like any other function.
So our function do stuff takes one argument core data.
So I'm going to pass in a boolean value.
That's true so that if block of our function runs so we call the function and then we call then function
with a dot operator like.
So dot then which is a function that's just part of the chain and then takes our two callbacks.
The first one is fine if everything goes well.
So we call dark then which is a function and pass it our Frist function.
If everything goes well that simply brings the message in the output.
Now let's go ahead and run this to see how it goes.
We should see the Sukses message printed in the output Arsa we can do stuff that doesn't actually do
anything.
It simply calls resolve if we pass in a boolean to as input.
So we see success message on the screen.
We can do the exact same thing with the errors.
So let's Ancrum in the reject part of the function the catch this error we must add a second argument
to our then function and this will receive our error.
And now we have two functions.
One when everything goes well and one then something goes wrong.
So if something breaks said call our function now to simulate a response real fast false or anything
other than Boolean to to our DO step function and you can see that the error message brings to the screen.
Another advantage of using promises is there is all and reject functions can only be called at most
once.
And contrast read callback functions if you accidentally invoke a callback function twice.
It will be executed two times.
However with promises even if you call the resolve or the rejet several times only the first call to
resolve or reject ever gets executed any subsequent calls to resolve or reject are always ignored in
the next lecture.
See how the chain these promises together and call them one after another.
So let's keep going.