Skip to content

Commit 79e65aa

Browse files
authored
Merge pull request #16 from writeonk/develop
asyn JS
2 parents d3edbbb + c1e26dd commit 79e65aa

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

Asynchronous JS/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
<script src="index1.js"></script>
9+
</head>
10+
<body>
11+
12+
</body>
13+
</html>

Asynchronous JS/index.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
// console.log(1)
3+
// console.log(2)
4+
// setTimeout(() => {
5+
// console.log("Function time out")
6+
// },2000) //2000 is in millisecond
7+
8+
// console.log(3)
9+
// console.log(4)
10+
11+
const getTodos = (resource) => {
12+
return new Promise ((resolve,reject)=>{ //request,reject are function
13+
const request = new XMLHttpRequest() // new is keyword used to create object, here object name is request
14+
15+
request.addEventListener('readystatechange', () => {
16+
if(request.readyState === 4 && request.status===200)
17+
{
18+
const data = JSON.parse(request.responseText)
19+
resolve(data)
20+
}
21+
else if(request.readyState === 4)
22+
{
23+
reject("could not fetch data")
24+
}
25+
})
26+
27+
request.open('GET',resource);
28+
request.send()
29+
})
30+
}
31+
32+
33+
// getTodos('todos.json',(err,data) => {
34+
// console.log(data)
35+
// getTodos('todos1.json',(err,data)=>{
36+
// console.log(data)
37+
// getTodos('todos2.json',(err,data)=>{
38+
// console.log(data)
39+
// })
40+
// })
41+
// })
42+
43+
getTodos("todos.json").then((data)=>{
44+
console.log(data)
45+
return getTodos("todos1.json")
46+
}).then((data)=>{
47+
console.log(data)
48+
return getTodos("todos2.json")
49+
}).then((data)=>{
50+
console.log(data)
51+
}).catch((err)=>{ //catch is to get get error
52+
console.log(err)
53+
})

Asynchronous JS/index1.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// fetch("todos.json").then((response)=>{ //fetch return promise
2+
// console.log('resolved',response)
3+
// return response.json() //respone.json return promise through which we get the data
4+
// }).then(data =>{
5+
// console.log(data)
6+
// }).catch(err =>{
7+
// console.log('error found')
8+
// })
9+
10+
const getTodos = async ()=>{ //async makes the function asynchronous and it return promise
11+
const response = await fetch('todos.json')
12+
// awaits stores the js and it stops the value to assign it to data until the promise has been resolve
13+
if(response.status !== 200) //if there is a problem in url
14+
{
15+
throw new Error('rejected') //when throw new error is executed then promis will return error and it will caught using .catch
16+
}
17+
const data = await response.json()
18+
return data
19+
}
20+
21+
getTodos() //function call
22+
.then(data => { //without .then we cannot get the value of respone
23+
console.log(data)
24+
})
25+
.catch(err =>{
26+
console.log(err.message) //display err message
27+
})

Asynchronous JS/todos.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"book":"abc" ,"author" : "xyz" ,"points": 5},
3+
{"book":"abc" ,"author" : "xyz" ,"points": 5},
4+
{"book":"abc" ,"author" : "xyz" ,"points": 5}
5+
]

Asynchronous JS/todos1.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"book":"abc" ,"author" : "xyz" ,"points": 5},
3+
{"book":"abc" ,"author" : "xyz" ,"points": 5},
4+
{"book":"abc" ,"author" : "xyz" ,"points": 5}
5+
]

Asynchronous JS/todos2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"book":"abc" ,"author" : "xyz" ,"points": 5},
3+
{"book":"abc" ,"author" : "xyz" ,"points": 5},
4+
{"book":"abc" ,"author" : "xyz" ,"points": 5}
5+
]

0 commit comments

Comments
 (0)