-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile142.js
More file actions
45 lines (39 loc) · 1.12 KB
/
Copy pathfile142.js
File metadata and controls
45 lines (39 loc) · 1.12 KB
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
38
39
40
41
42
43
44
45
const URL = "https://jsonplaceholder.typicode.com/posts";
function sendRequest(method,URL){
return new Promise( function (resolve,reject){
const xhr = new XMLHttpRequest();
xhr.open(method , URL)
xhr.onload =function(){
if(xhr.status >=200 && xhr.status <400){
resolve(xhr.response);
}else{
reject(new Error("something went wrong"));
}
}
xhr.onerror = function(){
reject(new Error("network error"));
}
xhr.send()
})
}
sendRequest("GET",URL)
.then(response =>{
const data = JSON.parse(response);
console.log(data);
return data;
})
.then(data =>{
const id = data[3].id;
return id;
})
.then(id => {
const url = `${URL}/${id}`
return sendRequest("GET",url);
})
.then(newResponse =>{
const newData = JSON.parse(newResponse);
console.log(newData);
})
.catch(Error =>{
console.error(error);
})