1fetch('http://example.com/songs')
2 .then(response => response.json())
3 .then(data => console.log(data))
4 .catch(err => console.error(err));
1// GET Request.
2fetch('https://jsonplaceholder.typicode.com/users')
3 // Handle success
4 .then(response => response.json()) // convert to json
5 .then(json => console.log(json)) //print data to console
6 .catch(err => console.log('Request Failed', err)); // Catch errors
1fetch('http://example.com/movies.json')
2 .then((response) => {
3 return response.json();
4 })
5 .then((data) => {
6 console.log(data);
7 });
8
1// Fetching random users
2const getRandomUsers = n => {
3 console.log('---- Random users generated ---');
4 const fetchRandomUsers = fetch(`https://randomuser.me/api/?results=${n}`);
5 fetchRandomUsers.then( data => data.json().then( randomUsers => {
6 console.log(JSON.stringify(randomUsers.results.length));
7 randomUsers.results.forEach( randomUserGenerated => {
8 const{gender , email , name} = randomUserGenerated;
9 const {title , first , last } = name ;
10 let fullNames = `${title}.${first} ${last}`;
11 console.log(`${fullNames} -- ${gender} -- ${email}`);
12 })
13 }));
14}
15getRandomUsers(100);
16