Asynchronous
Synchronous
- JavaScript executes code in sequence. If some code takes long time it will wait for it.
Asynchronous
- JavaScript will execute some code in asynchronous fashion. It will call the code and won't wait for its completion and proceed with execution of the other code.
- Many functions are provided by JavaScript host environments that allow you to schedule asynchronous actions. In other words, actions that we initiate now, but they finish later.
- Examples of asynchronous
- timeout function
- loading script dynamically
- sending http request using XHR, fetch
- You can handle the result asynchronous code using
- Callbacks
- Promises
- Async Await
Timer
- Timer codes are executed asynchronously
setTimeout
Sets a timer which executes a function or specified piece of code once the timer expires.setInterval
: Repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.