Asynchronous JavaScript
The JavaScript Language
- Synchronous Language
- Single threaded language
- Code runs line by line
- Each operation must wait for the former to finish
Execution Context
-
Two Phases
- Creation Phase
- Execution Phase
-
Creation Phase
- Create the global object (browser = window, node.js = global)
- Create the 'this' object and bind it to the global object
- Setup the memory heap for storing variables and function references
- Store the functions and variables in the execution context and set to 'undefined'
-
Execution Phase
- Code is executed line by line
- A new execution context is created for each function call
-
The Call Stack
- A mechanism for the JavaScript interpreter to keep track of its place in a file that calls multiple functions
- Each execution context is put onto the call stack.
Call Stack Example
const first = () => {
console.log('first...');
second();
}
const second = () => {
third();
console.log('second...');
}
const third = () => {
console.log('third...');
}
first();
- Steps
- The order of Execution Context (EC's) put onto the call stack
- Push Global EC
- Push First EC
- Push Log EC, Execute ('first...') then Pop Log EC
- Push Second EC
- Push Third EC
- Push Log EC, Execute ('third...') then Pop Log EC
- Pop Third EC
- Push Log EC, Execute ('second...') then Pop Log EC
- Pop Second EC then First EC and finally the Global EC
Asynchronous Programming
-
Technique that enables the program to start a potentially long running task and still be able to respond to other events
-
The Task Queue and Event Loop
- The task queue contains registered callback functions from asynchronous operations
- The event loop checks if the call stack is empty, if so it pushes the registered callback to the stack
- These callback functions are then executed and popped of the stack
Web API Callback Example
- Process of an asynchronous call to a web API
- setTimeout function executed and popped off the stack
- The web API that handles the setTimeout function registers a given callback
- The web API does some work, in this case the system must wait x amount of milliseconds
- Registered callback is put in the macro task queue
- Event loop sees there is a callback in the task queue and checks if the call stack is empty
- Once the call stack is empty teh event loop signals that teh callback function can be put on the stack
- The callback is executed and popped off the stack
Promises Callback Example
- Process of an asynchronous call promises are involved
- Resolve function is pushed on the call stack, executed and popped off
- The resolve function returns a promise that is 'thenable'
- The Then() function is pushed onto the call stack and immediately popped off
- A callback function passed as an argument to the then function is registered
- This callback is enqueued to the micro task queue (A.K.A the promise jobs queue)
- The event loop prioritizes the micro task queue over the macro task queue and pushes the promise callback to the call stack for execution
- Order of addition to the callstack
- Synchronous operations that do not have to register callbacks
- Asynchronous promise registered callbacks
- Asynchronous web API registered callbacks