|
| 1 | +## ⚑ Asynchronous JavaScript: |
| 2 | +Asynchronous programming in JavaScript allows tasks to run independent of the main thread execution without disturbing it. This performs operations independent of the main thread execution like fetching data from a server, handling user input, etc., |
| 3 | + |
| 4 | +*It performing time-consuming tasks without freezing the UI.* |
| 5 | + |
| 6 | +*These operations don't block the main thread and allowing other parts of your code continuously executing while the asynchronous task is in progress.* |
| 7 | + |
| 8 | +### ☴ Overview: |
| 9 | +1. [Callbacks](#-callbacks) |
| 10 | +2. [Promises](#-promises) |
| 11 | +3. [Async/Await](#-async-await) |
| 12 | + |
| 13 | +### ✦ Callbacks: |
| 14 | +Callbacks are functions that are passed as arguments to other function calls. This callbacks are executed at a later or end of the function or typically when an asynchronous operation completes. |
| 15 | + |
| 16 | +```javascript |
| 17 | +fetch('https://api.example.com/data') |
| 18 | + .then(response => response.json()) |
| 19 | + .then(data => { |
| 20 | + console.log(data); |
| 21 | + }) |
| 22 | + .catch(error => { |
| 23 | + console.error('Error:', error); |
| 24 | + }); |
| 25 | +``` |
| 26 | + |
| 27 | +In this above example, |
| 28 | +- *The `fetch()` function returns a Promise,* |
| 29 | +- *That promise is then resolved with the response data.* |
| 30 | +- *The `.then()` method is used to handle the resolved value.* |
| 31 | +- *The `.catch()` method is used to handle potential errors.* |
| 32 | + |
| 33 | +### ✦ Promises: |
| 34 | +Promises are objects that represent the eventual completion or failure of an asynchronous operation. They provide a more structured and readable way to handle asynchronous code compared to callbacks. |
| 35 | + |
| 36 | +```javascript |
| 37 | +function fetchData() { |
| 38 | + return new Promise((resolve, reject) => { |
| 39 | + setTimeout(() => { |
| 40 | + resolve('Data fetched successfully'); |
| 41 | + }, 2000); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +fetchData() |
| 46 | + .then(data => { |
| 47 | + console.log(data); |
| 48 | + }) |
| 49 | + .catch(error => { |
| 50 | + console.error('Error:', error); |
| 51 | + }); |
| 52 | +``` |
| 53 | + |
| 54 | +In this above example, |
| 55 | +- *Promises can either be resolved or rejected.* |
| 56 | +- *The `.then()` method is used to handle the resolved value.* |
| 57 | +- *While the `.catch()` method is used to handle rejection.* |
| 58 | + |
| 59 | +### ✦ Async Await: |
| 60 | +Using `async` and `await` for Cleaner Asynchronous Code. |
| 61 | +- `async`: Declares a function as asynchronous. |
| 62 | +- `await`: Pauses the execution of an `async` function until a Promise is resolved. |
| 63 | + |
| 64 | +```javascript |
| 65 | +async function fetchData() { |
| 66 | + const data = await fetch('https://api.example.com/data'); |
| 67 | + const json = await data.json(); |
| 68 | + console.log(json); |
| 69 | +} |
| 70 | + |
| 71 | +fetchData(); |
| 72 | +``` |
| 73 | + |
| 74 | +--- |
| 75 | +[⇪ To Top](#-asynchronous-javascript) |
| 76 | + |
| 77 | +[❮ Previous Topic](./arrays.md)   [Next Topic ❯](./error-handling.md) |
| 78 | + |
| 79 | +[⌂ Goto Home Page](../README.md) |
0 commit comments