-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
30 lines (27 loc) · 844 Bytes
/
request.js
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
const getTodos = (resource) => {
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.addEventListener('readystatechange', () => {
if(request.readyState === 4 && request.status === 200){
const data = JSON.parse(request.responseText);
resolve(data);
}
else if(request.readyState === 4) {
reject('Could not fetch data');
}
});
request.open('GET', resource);
request.send();
});
};
getTodos('todos/jaid.json').then(data => {
console.log('Promise 1 resolved:', data);
return getTodos('todos/hugo.json');
}).then(data => {
console.log('Promise 2 resolved:', data);
return getTodos('todos/joseph.json');
}).then(data => {
console.log('Promise 3 resolved:', data);
}).catch(err => {
console.log('Promise rejected:', err);
});