[MUSIC]
>> Hi there, I'm Christopher Harrison,
Senior Program Manager, Microsoft Cloud and AI.
Let's take a look at the code at a demo for how we can use
promises inside of JavaScript to manage long running operations.
Now, the way that we're going to do this is
the same way that we did it in the slides,
which is we're going to take what was built for
callbacks and convert that
into something that could be used with promises.
Inside of JavaScript, we've had for
a very long time this little thing called setTimeout,
which will take a callback that it's going to
call once it's done running.
Well, we want to do that with a promise.
I'm going to say function and I'm going
to call this promiseTimeout,
and we'll ask for the milliseconds,
and then we'll go ahead and say, "return new Promise".
Remember, of course, that when we're talking about our Promises,
it's all based around this Promise object.
Now, the Promise object, the constructor here,
what it is that we're going to use to create this is
going to be looking for the logic.
It's going to ask us to put whatever it is that
we want to have happen right inside of that function there.
We'll do this by setting up an anonymous function.
Now, the Promise will go ahead and call this and it will give us
two little parameters that will
allow us to indicate success or failure.
Resolve is going to be called to indicate success,
and reject is called to indicate failure.
Once we know that things succeed,
we could just go ahead and say, "resolve".
If we wanted to pass in a value,
we could go ahead and do that,
and voila, the Promise has now succeeded.
Reject would simply indicate that it failed.
Now, because of the fact that resolve and
reject are nothing more than functions,
we can use that inside of setTimeout.
I'm going to say setTimeout and call resolve.
That's going to resolve or complete, if you will,
the promise once the timer has
elapsed and the timer is going to be set
to the number of milliseconds that were passed in.
Now, I can use this.
Now, I can go ahead and say, "promiseTimeout".
Specify value, let's say, "2000",
and our then is going to be on success.
When things succeed, we'll go ahead and say,
"console.logdone('Done!!')"
Should things fail, then we can have our catch.
Our Catch will then have a very complex
set of error handling, "console.log('Error!')".
In your real applications,
you probably want something more than that.
All right. Let me go ahead and save that.
Now, I'm going to go ahead and say, "node promise-demo.js"
Cool. When I run this, two seconds will go by and we'll get done.
Now, just to highlight the fact that our reject is
going to raise an error and that would wind up inside of Catch.
Let me change my code here to call reject.
Now, after that two-second delay,
you're going to notice that it gives us that error.
All right. Now, one other thing that's worth
highlighting here is the fact that we
could go ahead and continue to string these together.
Let me make sure I change that back to resolve here.
I could, right here,
go ahead and close that off, ".then",
and let's set up another one right here.
Little bit of clean up, perfect,
and then, "console.log('Also done!!')". Cool.
Now, I could go ahead and say, "return",
and let's say, "promiseTimeout," and then say, "1000".
Now, what's going to happen is this first part is going to run.
Then the second, and that will then execute,
or the response to it, will then execute on the next then.
This is something that you're going to see quite commonly in code.
Because quite frequently, what you'll
do is you'll make a call out to one service,
get a response, have to do something with it.
Then maybe callback to save the value,
maybe call out to another service.
It's quite common to go then, then, then the whole way down.
Now, I also want to highlight the fact,
and let me just do a real quick copy and paste here,
we could pass values down
or resolve a promise by using a little helper here.
I'm going to say resolve 42.
Now, I'll put in my results here,
and now, we'll go ahead and say, "console.log",
and then the, "(result)",
so that result will be whatever is inside of the resolve.
Now, when I run this,
what we're going to notice is we're going to see,
"('Done!!')", ('Also done!!')", and then that number, "(42)".
That is how we can utilize Promises in
code to manage our long-running asynchronous operations.