What is asynchronous code?
Asynchronous code differs from synchronous code in that it doesn’t wait for a particular line of code to execute before it continues on to execute the next line of code.
For example, if a request was made to an external server, a synchronous function would wait for the request to be executed and returned before moving on to execute the next function.
Asynchronous code however, would send off the request, and immediately continue on to execute the next function before waiting for a response from the previous function.
Describe what a callback function is
A callback function is a function that is passed into an asynchronous function which is called once the task of the parent asynchronous function is completed.
For example, in the instance of the following asynchronous function:
getWeather(function(weather) {
console.log("The weather is: " + weather);
});
The asynchronous getWeather()
function could be call synchronously, however inside of it is function(weather)
which is called once we have getWeather()
.
So, first we get the weather, and then once we’ve got it, we call the callback function inside the parent getWeather()
function, and execute the callback functions instructions…
Which in this instance is console.log("The weather is: " + weather);
Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
Asynchronous functions are vital for a number of reasons, most of which is arguably to do with efficiently serving requests.
If synchronous code is unable to execute, everything else must wait (sometimes indefinitely). No more executions can go ahead until the bottleneck is cleared.
This can be detrimental for servers which deal with many requests from many clients.
If there is any bottleneck or waiting period for one client, every following request thereafter must wait until the bottleneck is executed.
So with asynchronous code, we can set the wheels in motion, and then continue with other tasks. And then once the asynchronous functions or getters are returned, then we can return to the following instructions with the data that was returned to us.
It is a significantly more efficient way of processing code when you have requests to external systems which don’t require the compiler to use all resources in executing the function at hand.
What are Promises?
Promises are a way in dealing with callback hell, which is a visually taxing phenomenon when dealing with multiple asynchronous functions which rely on one another to be performed.
These call functions can nest many layers deep, and when a developer attempts to modify some of the code, it can be tricky to decipher, which is more error prone.
Promises are an elegant solution to this callback hell in that instead of using functions which accept an input and a callback, a developer can make a function that returns a promise object.
A promise object is an object that represents some value that the program will obtain in the future.
What does the then() function do?
A then()
function is similar to the callback function in that it tells the program what to do after we get the returned information from the promise object.
So, as an example, a program will begin to fetch some data from an API call, and in the promise set by the initial function, and once we receive a response (if it resolves), we have a .then(function(nextFunction))
to continue on with the information we now have.