Instructor: The first technique we'll discuss
is the Async Operations.
The basic premise of Async Operations
is that the code should not wait
for long operations to complete, but instead
the code begins a long operation and continues on
and when the operation is done, then the code can continue
from the point where the operation completed.
Now, this is relevant mainly for IO operations
such as operations related
to files, to database access, to networking, et cetera.
So if you have an operation that requires reading
dozens of files, then using the Async Operation
you can instruct the code to begin the reading operation.
Meanwhile, continuing performing other operations
and when the file reading is complete
you can continue straight from the point
in the code where the reading is done.
Now, almost every development platforms
supports this concept nowadays, and let's see an example.
And what you see here is an implementation
of the Async await pattern in node.js.
There are two things to know in this code.
First is the a wait keyword
which signals the code to begin this operations
the fetch operation, which is a long operation.
And while the code is fetching the data
from this address, then the calling method
can continue doing whatever they did before.
Now for this to happen, there is also the Async keyword.
So the Async keyword, signals the calling method
that this function, the Async Ajax await function
is an Async function,
which means it might have an await Q ordinate
so the calling function might continue do something
after calling the Async Ajax await.
And again, the I Async await pattern exists
in almost every development platform.
This pattern is a very powerful one
and you should always aspire to use it.
It frees the threads on the web server
and it dramatically improves the performance of the API.