-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch.js
More file actions
52 lines (43 loc) · 1.22 KB
/
Copy pathfetch.js
File metadata and controls
52 lines (43 loc) · 1.22 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
46
47
48
49
50
51
52
const requestURL = "http://jsonplaceholder.typicode.com/users";
function sendRequest(method, url, body = null) {
return fetch(url, {
method: method,
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" }
}).then(response => {
if (response.ok) {
return response.json();
}
return response.json().then(error => {
const someError = new Error("Some error..");
someError.data = error;
});
});
}
// sendRequest("GET", requestURL)
// .then(data => console.log(data))
// .catch(error => console.error(error));
const someObject = {
prop1: "One",
prop2: 1
}
sendRequest("POST", requestURL, someObject)
.then(data => console.log(data))
.catch(error => console.error(error))
async function sendRequestAsync(model, url) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(model)
});
const data = await response.json();
return data;
}
async function fetchRequest() {
const model = {
prop: "value",
}
const result = await sendRequestAsync(model, url);
}