-
Notifications
You must be signed in to change notification settings - Fork 0
/
66_fetch_api.js
38 lines (30 loc) · 1.24 KB
/
66_fetch_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
//when we fetch api, a promise is created and it is
//resovles in another promise whose value is finall result
let p =fetch('https://goweather.herokuapp.com/weather/london');
//getting a response is a two stage process
//1->an object of response{..} is created with some properties
//ex-> state=> https status code / ok=> boolean true if status code is 200-299
p.then((response)=>{
//response object{...} some properties
console.log(response);
//status code
console.log(response.status)
//response headers
console.log(response.headers);
//boolean true or false(200-299);
console.log(response.ok)
//gives error
// console.log(response.text());
//a new promise is return to next then() to resolve
//to acesses the body in defined format
//.json=> parse the response in json(js object)
//.text()=> read and return the text ... etc...
//we can only use one body reading mehtod..
//promise will resolve here ans pass to next then for final result
return response.json();
}).then((response)=>{
//it will give me the body of api
console.log(response);
})
//we set the request headers and print response headers
//to set a request header in fetch we can use header options in fetch syntax