【JavaScript】Asynchronous JavaScript
Synchronous
Synchronous means the code is executed line by line, and each line of code waits for previous line to finish.
But if some code will take some time to finish, it might block the thread, so we need a way to solve this problem.
Asynchronous
Asynchronous code like timer run in the background and register a callback function, the callback run after the timer has finished.
And the main code is not being blocked, and execution doesn’t wait for the asynchronous timer to finish its work.
Image is also loading in the background, once the image has finished loading, a load event will be emitted by JavaScript.
And finally the callback function is executed.
AJAX
AJAX allows us to communicate with remote web servers in a asynchronous way.
In practice, we make AJAX calls in our code in order to request some data from a web server dynamically.
How AJAX work?
Our JavaScript application running in the browser, which is also called Client.
With AJAX, we can do HTTP request to the server which has the data we want to fetch.
And the server will then send back a response contains the data we requested.
And the process between Client and Server all happens asynchronously in the background.
API
When we’re asking a server to send us some data, the server usually contains a web API.
API is basically a piece of software that can be used by another piece of software in order to allow applications to talk to each other and exchange information.
There are many types of APIs, but the most important type of API can be called Online API or just API.
Online API is an application running on a web server, which receives request for data, then retrieves the data from some database, and sends back to the Client.