Understanding Asynchronous Programming?
Synchronous programming executes one line of code at a time. When the code line executes, it requests the result. The application waits for the response. Once the response returns, the next line of code fired off!
Click through the slide below to see the concept illustrated.
// code 1 let num = 8; // code 2 let triple = num * 3; // code 3 console.log(triple);
In contrast, asynchronous code does not wait. Code statements will fire off one by one, like with synchronous code. However, the application does not wait for a returned response before moving on to the next code block.
Clcik through the slide below to see the concept illustrated.
- Ajax Requests
- Reading/Writing Files
- Timer Functions
Asynchronous code requieres a callback. to handle the results. A call back is a function that is passed into another function. If you pass a callback into an asynchronous function, that callback will wait for a response from the callback.
What is the Benefit of Asynchronous Code?