-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_talk2servers.js
125 lines (104 loc) · 3.57 KB
/
5_talk2servers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
JSON : js object notation
bentuknya mirip banget dengan objek di js tetapi belum tentu valid itu objek js.
tetapi server biasanya tidak mengirim dalam bentuk objek tetapi berbentuk string
*/
/*
const responseFromServer = `{"name": "Luna","age": 10,"breed": "Havanese","location": {"city":"Seattle","state": "WA"}}`;
console.log(responseFromServer); // output string
// maka perlu di parse dengan fungsi JSON builtin
// fyi JSON selalu dobel quote, tidak bs single quote atau backtic
const parseResponse = JSON.parse(responseFromServer);
console.log(parseResponse);
// kalo sudah diparse ke objek baru bs dipanggil objeknya
console.log(parseResponse.name);
console.log(parseResponse.age);
console.log(parseResponse.location);
// untuk mengkonversi objek JS ke JSON, menggunakan JSON.stringify()
const dataClient = {nama: 'jokonto77', usia: 69};
const dataToSend = JSON.stringify(dataClient);
console.log(dataToSend);
*/
/*
AJAX
API : adalah url yang dapat membuat request.
API seperti website tetapi hanya utk machine.
metodnya dapat merekues ke komputer utk mendapatkan informasi
*/
const DOG_URL = "https://dog.ceo/api/breeds/image/random";
const dogDisplay = document.getElementById('dog-target');
/*
function addDog(){
const asu = fetch(DOG_URL);
// console.log(asu);
asu.then(function (response){
// const proccesingPromise = response.text();
const processingAnotherPromise = response.json();
// return proccesingPromise;
console.log(processingAnotherPromise);
return processingAnotherPromise;
})
.then(function(processedResponse){
// const dogObj = JSON.parse(processedResponse);
const img = document.createElement('img');
// img.src = dogObj.message;
img.src = processedResponse.message;
img.alt = 'cute dog';
dogDisplay.appendChild(img);
}).catch(function(error){
alert('it is error !')
});
}
document.getElementById('dog-btn').addEventListener('click', addDog);
*/
/*
menggunakan response.text
function addDog() {
const promise = fetch(DOG_URL);
promise
.then(function (response) {
const processingPromise = response.text();
return processingPromise;
})
.then(function (processedResponse) {
const dogObject = JSON.parse(processedResponse);
const img = document.createElement("img");
img.src = dogObject.message;
img.alt = "Cute doggo";
doggos.appendChild(img);
});
}
*/
// bedanya adalah ketika kita menggunakan response.text(),
// kita harus melakukan JSON.parse
// ketika response yang diambil adalah response.json(), kita
// tidak perlu lagi melakukan parsing dan langsung memanfaatkannya
// selain kode diatas, kita dpat menggunakan async await alih alih
// menggunakan banyak chaining .then dan .then
async function addDog() {
const janji = await fetch(DOG_URL);
const prosesRespon = await janji.json();
const img = document.createElement('img');
img.src = prosesRespon.message;
img.alt = 'kyut dog';
dogDisplay.appendChild(img);
}
document.getElementById('dog-btn').addEventListener('click', addDog);
// keyword await hanya bisa digunakan di dalam fungsi async.
// jadi berikan keyword async dulu ke fungsi yang akan diberi await
// await = tell your code to wait or pause until they finished that job
// contoh :
/*
function getName(){
return 'brian';
}
console.log(`a promise ${getName()}`);
*/
// output : a promise brian
// hanya string biasa
// sedangkan jika fungsi getName diberi async akan return promise
async function getname(){
return 'brian';
}
console.log(`output : ${getName()}`)
// output : output : [object Promise]