File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed
js-core/classworks/classwork-21 Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Class work 21</ title >
6+ </ head >
7+ < body >
8+ < script src ="src/main.js "> </ script >
9+ </ body >
10+ </ html >
Original file line number Diff line number Diff line change 1+ const url = 'https://jsonplaceholder.typicode.com/users/' ;
2+
3+ const getUser = ( userId , callback ) => {
4+ const xhr = new XMLHttpRequest ( ) ;
5+ xhr . onreadystatechange = ( ) => {
6+ if ( xhr . readyState === 4 ) {
7+ const user = JSON . parse ( xhr . response ) ;
8+ callback ( user ) ;
9+ }
10+ }
11+ xhr . open ( 'GET' , `${ url } /${ userId } ` , true ) ;
12+ xhr . send ( ) ;
13+ }
14+
15+ getUser ( 1 , user1 => {
16+ getUser ( 2 , user2 => {
17+ getUser ( 3 , user3 => {
18+ console . log ( user1 . id + user2 . id + user3 . id ) ;
19+ console . log ( user1 . name + user2 . name + user3 . name ) ;
20+ } )
21+ } )
22+ } )
23+
24+
25+ // const myPromise = new Promise((resolve, reject) => {
26+
27+ // })
28+
29+ const users = [ 1 , 2 , 3 ] ;
30+
31+ const usersPromise = users . map ( user => {
32+ return fetch ( url + user ) . then ( response => response . json ( ) ) ;
33+ } )
34+
35+ Promise . all ( usersPromise )
36+ . then ( allUsers => {
37+ console . log ( allUsers ) ;
38+ } )
39+
40+ const url2 = 'http://easycode-js.herokuapp.com/ollu/users' ;
41+
42+ fetch ( url2 )
43+ . then ( data => {
44+ return data . json ( )
45+ } )
46+ . then ( users => console . log ( users ) ) ;
47+
48+ fetch ( url2 , {
49+ method : 'POST' ,
50+ headers : {
51+ 'Content-type' : 'application/json'
52+ } ,
53+ body : JSON . stringify ( {
54+ fullName : 'Leon Kara' ,
55+ 56+ phone : '00000000'
57+ } )
58+ } )
You can’t perform that action at this time.
0 commit comments