- [Instructor] Both promises and callbacks
can be used to handle asynchronicity within JavaScript.
Promises make the code a little bit nicer,
so Node JS comes with a way to quickly
convert callback functions into promises.
I'm inside of the exercise files for chapter one,
chapter one, lesson four, and in the start
folder, our Index dot JS has our
delay function that uses callbacks.
This delay function also incorporates errors.
So the Node JS rule is that errors
are always passed to the callback
function as the first argument.
So when the time is two seconds,
and we go ahead and run this script
by type Node dot, we can see that
the two second delay is over.
We are simply just logging the message,
and we can see that the callback is called
and passes the message as the second argument.
This means that an error hasn't
occurred because the error is null.
If we actually up the time to, say,
four seconds, and then call this
function, then we have an error.
So the rule is that we pass the error
as the first argument to the callback,
so in this case, if errors are passed
as the first argument, we can assume
that they are errors and handle them.
Now, if our callback functions are all structured
like this, where they pass the error
as the first argument to the callback,
and they pass any additional values
as second, or third, or fourth arguments
to the callback, then we can quickly
convert these functions into promises
using a utility that ships with Node JS.
Let's go ahead and go to the top of this
script and add a var for promisify.
So promisify is a function that we can
use to convert callback functions into promises.
You can find it in the util package
that ships with the Node JS core.
So now that I have this promisify function,
what I can do is use this delay
function, but turn it into a promise.
Var promise delay equals promisify,
and we'll go ahead and send it the delay function.
So now, we can actually use this method as a promise.
So I'll go ahead and call promise, delay
dot two dot then, we'll go ahead and
just call it console dot log, so
the console log will handle whatever
this promise resolves to.
We'll just log it to the console.
So if I come over here into the terminal
and do node dot, we see that the two second delay is over.
I can also throw a dot catch, and we can grab our error,
and I can go ahead and console dot log that
as error and we'll go ahead and put
the error dot message to the console,
so let's go ahead and save that,
and then I can come back over to my terminal,
and we'll go ahead and run this script
again with node dot, oh, and the two
second delay is over, I didn't up it
to a number like five that will cause an error,
so we will now invoke this promise
delay with five, give that a shot,
and we see error, error message.
So I've made a second mistake here.
This is a common mistake that I make.
We're not using single quotes when we
do template strings, we're using backticks,
so let me go ahead and make sure I surround
that string with backticks so that
we can see the error message.
We'll go ahead and clear my terminal
and we'll try this one more time.
There we go.
The five second delay is too long.
So this utility promisify is very useful and very effective.
Let's take a look at how we can use the same
utility with the Node JS file system.
So I'm gonna go ahead and just delete all of this code,
and I'm also gonna pull in the var fs.
So we'll say fs equals require.
The fs, that's our file system module,
and our file system module, all of the functions
use callbacks, but let's say we want it
to create a writefile function that uses
a promise instead of a callback.
So I can promisify fs writefile
because it follows that Node JS pattern.
And the errors that occur in the writefile
are passed at the first argument
to the callback, and if any errors do not occur,
any other data is passed at the second,
third, or fourth arguments to the callback.
So this promisify utility will know
how to automatically convert that writefile
function into a promise, so now I can
call writefile, and we'll say sample
dot text, and the value that we will
write to the file is this is a sample,
and we don't have to use a callback,
because now it's a promise, so when
the file has been successfully written,
we'll log a message to the console
that says file successfully created,
and if anything goes wrong, we can
handle that with our error method, error creating file.
Go ahead and cap that with a semicolon,
and then we can go ahead and give this a shot.
We'll run this by typing node dot,
it says file successfully created,
and then when I do LS we can see
that we have a sample dot text file.
In fact, I can do command backslash
within atom, and we can take a look
at that file and we can see that we wrote the sample file.
So the promisify method that's found
in the utilities module is a great
tool for turning callback functions into promises
so that they're a little bit easier to work with.