In this video, I'm going to show you how we can make use of async await because Node.js relies solely
on callbacks.
But lucky for us, with the new versions of Node.js, we can now use a new feature in JavaScript, for
example, async await.
All right, so let's see how we are going to do it.
So back to my video.
I studio call this where we left by using callback to create a file and write content onto the file.
All right, so let's read more about it.
So let's scroll down and let's locate the file system and this what we want.
Let's scroll down and read more about how we can make use of async await.
So down here you can see that we have what is called promise example.
And before I move on, you can see that for this particular module we can import it in two different
ways.
One is using the synchronous way and the next one is using the promises.
And for the import and export, we have two types.
The first one is called the Common JS, and then the ESM is a new feature like import and export.
But we are going to use the common JS way.
As you move on, I'm going to show you how we can make use of import and export and to use the new features
like import and export that is a ESM, we need some few lines of configuration.
So let's stick to the common JS way and you can see that to be able to use promises, this is how we
need to require the module, the module name and then slash promises.
So let's get back to the code and see how we are going to recreate this functionality using async await.
So down here I have a comment called create a file using async await.
So let's go through this code.
Let me uncomment it and let's go through step by step to show you that indeed it's using callback here.
You can see that this is the actual function and this function takes in three arguments.
The first one is a file that we want to create, and this one is going to be the content that you want
to write onto this file and the callback function here is going to run after the operation is done.
So in case there are some error, we are going to catch it here and in case is everything is okay.
You want to display some text to the user.
So let's see how you can make use of async and we to simplify the syntax a little bit.
So as we saw on the documentation, the way we require this one, I'm going to change.
So let me copy this one and comment it out here and now and create a file using async await.
Let me bring it here and now to use promo is going to be slash promises.
As that.
And remember, I think a way it works with the function.
So we need to create a function to create a file.
So down here I'll make use of const create.
Five.
For the meantime, this file will not take in any agreement.
And there you go.
So to be able to use async await.
First of all, we need to mark our function as an async.
So here I'll make it as an async.
And inside the next step is our branch might try and catch inside.
My try is where my actual logic is going to be and then inside a cards is where it's going to throw
an error in case there will be one.
So inside here are what I need to do is await the operation.
If you read the documentation, it says that promise based operations return a promise that is fulfilled
when the asynchronous operation is complete.
So we need to await that.
So here we bring in the first module.
F's dot right file.
And for this one, it takes into agreement instead of three.
So the first agreement going to be the file that I want to create.
So the file I want to create, I would name it as log in towards a mirror and then I want to write a
simple test on to it.
Say that.
Welcome to your.
Logan Paige.
All right.
And that is it.
And inside a catch.
I'm going to throw error in case there will be one I start.
The next step is I need to call the function called create file and that is it.
So how can I run this operation or my Node.js application?
As usual, you can use your built in terminal or you can use the terminal that came with visualized
the code.
So I'm going to use the one that came with visual seed code.
I'll click on New Terminal here and there you go.
Or better still to make your life easier.
Always the fact that you want to run right click and then click on Open in Integrated Terminal, open
it.
And now we are in the folder where we want to run our file.
I have two instances of the terminal, the first one, second one.
So let me close this one.
All right, but I want to move my termina to this side here.
That's how I want it.
So I'll click on this, and then wherever I want to move it, or just release my mouse and there we
go.
So here, how to run, as we did in the previous video node and the file name.
That is my F.
S, which is inside here.
So let me hit enter.
And now where I get some error, it says that cannot access F.
S before initialization.
So where?
Oh, sorry.
So I need to uncomment this one, this one for the callback way.
So now let's click the console again, and now let's run the application.
And now something has happened.
You can see that there is no message because inside our try we are not displaying any message.
But lucky for us, if you check it here, you can see that it has what created with the content in it.
So let me delete again and now let me run application and send some text.
So inside the try or after the operation then I want to console.log.
Simple text is that file created?
So now let's run again and now see that I see file created if I check.
And indeed the file has been created.
All right.
So that is it about how we can make use of async await.
We can customize this function to create any file with a PDF or txt or docs, whatever.
So for this one, I'll copy this one.
And now come down here.
And now the first function call.
So this function you are going to pass in the file that you want to create.
So I'll call this one file to create and then the contents are called.
This one is the contents that I want right to the file.
So now inside my right file, instead of this static text here, I'm going to replace this one with
a file to create.
And the next element going to be the content.
I want to create our pass in here as my content to create.
So let's say I want to create PDF file.
Right.
So our code is going to be about.
As a PDF.
Now I want to write something and say that this is my first PDF file created perfect.
So now let's round the application and let's see where I guess I'm error.
It says that this is my about us is not defined.
Oh, sorry.
I had to put them in quotes.
Sorry, guys.
Yes, sorry, guys.
I believe you guys.
Sorry.
So let me clear the console and run again.
And now we see that file created.
Let's check it out.
If indeed has created and indeed we have PDF file being created.
See how we're able to customize this function to create any file we want.