Skip to content

Commit 65c0c09

Browse files
committed
Added asynchronous javascript
1 parent 23de13e commit 65c0c09

File tree

2 files changed

+83
-14
lines changed

2 files changed

+83
-14
lines changed

README.md

+4-14
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,10 @@ This repository contains Vanilla JavaScript topics and their notes to learn from
8282
2. [Arrays:](./docs/arrays.md)
8383
- [Array methods](./docs/arrays.md#-array-methods)
8484
- [Array iteration](./docs/arrays.md#-array-iteration)
85-
3. Asynchronous JavaScript:
86-
- Callbacks
87-
- Understanding asynchronous operations
88-
- Using callbacks to handle asynchronous results
89-
- Promises
90-
- Creating and chaining promises.
91-
- Handling promise resolution and rejection
92-
- Async/await
93-
- Using `async` and `await` for cleaner asynchronous code.
94-
4. Modules:
95-
- CommonJS modules
96-
- ES modules
97-
- Importing modules
98-
- Exporting modules
85+
3. [Asynchronous JavaScript:](./docs/asynchronous-javascript.md)
86+
- [Callbacks](./docs/asynchronous-javascript.md#-callbacks)
87+
- [Promises](./docs/asynchronous-javascript.md#-promises)
88+
- [Async/Await](./docs/asynchronous-javascript.md#-async-await)
9989

10090
### Advanced Topics
10191
1. Error Handling:

docs/asynchronous-javascript.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)