what_is_asynchronous_javascript.md - z22756392z/wp109b GitHub Wiki

Asynchronous JavaScript

-Governs how we perform task which take some time to complete

(e.g.Getting data from a database)

Start something now and finish it later

this is the pattern we generally want to follow when running tasks that take some time to do.

Like network requests for data to a database or an api.

We pass the asynchronous function or the statement some kind of callback function as a parameter

And then that callback function is the thing that runs and finishes later on once the request is complete and the data comes back.

And what it mean is that:

the browser takes that request and it handle it outside of the scope of this single thread in another part of the browser

it also takes a callback function and puts it to one side too so that it knows to execute this later on,

when the data comes back.

So because this network requests has been taken out of this thread and is now running in different part of the browser javascript can carry on down the queue and run the remaining functions all the while this is still going on the request for data.

So it continue through these functions and then when it receives the data back from the network request and once the rest of the functions have been executed. Then we're allowed to call this callback function and finish this original function


Synchronous JavaScript

JavaScript can run one statement at a time


image