-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS.Fetchung and api.js
62 lines (43 loc) · 1.36 KB
/
JS.Fetchung and api.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// async function getData(){
// // simulate getting data from a server
// return new Promise((resolve, reject)=>{
// setTimeout(()=>{
// resolve(455)
// }, 3500);
// })
// }
// settle means resolve or reject
// resolve means promise has settled successfully
// reject means has not settled successfully
async function getData() {
// simulate getting data from a server
// let x = await fetch('https://jsonplaceholder.typicode.com/todos/1')
let x = await fetch('https://jspnplaceholder.typicode.com/posts',{
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'apllication/json; charset=UTF-8',
},
})
let data = await x.json()
return data
}
async function main(){
console.log("Loading modules")
console.log("Do something else")
console.log("Load data")
let data = await getData()
console.log(data)
console.log("process data")
console.log("task 2")
}
main()
// data.then((v)=>{
// console.log(data)
// console.log("process data")
// console.log("task 2")
// })