-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5. Callback_hell.js
More file actions
37 lines (29 loc) · 865 Bytes
/
5. Callback_hell.js
File metadata and controls
37 lines (29 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// JSON Objects
// in luigi.json -> [{"text":"hey"},{"text":"hi"},{"text":"hello"}]
// in maria.json -> [{"text":"bye"},{"text":"sayonara"},{"text":"annyeoung"}]
// in deba.json -> [{"text":"bleh"},{"text":"duh"},{"text":"haa"}]
const Todos = (resource, callback)=>{
const request = new XMLHttpRequest();
request.addEventListener('readystatechange',
()=>{
if(request.readyState===4 && request.status===200){
const data = JSON.parse(request.responseText)
callback(undefined,data)
}else if(request.readyState===4){
callback('could not fetch data',undefined)
}
}
)
request.open('GET',resource);
request.send();
}
//Callback hell --> too many callbacks :(
Todos('luigi.json',(err,data)=>{
console.log(data);
Todos('maria.json',(err,data)=>{
console.log(data);
Todos('deba.json',(err,data)=>{
console.log(data);
})
})
})