-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (84 loc) · 2.75 KB
/
index.js
File metadata and controls
92 lines (84 loc) · 2.75 KB
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
function wait(time, error) {
return new Promise(function (resolve, reject) {
setTimeout(() => {
if (error)
return reject("FAILED")
resolve("DONE")
}, time);
});
}
wait(1500).then(text => {
console.log(text);
return wait(4500);
}).then(text => {
console.log(text)
return wait(1500, true);
}).then(text => {
console.log(text);
}).catch(e => {
console.log(e)
})
async function waitOnThings() {
const d1 = await wait(1500);
const d2 = await wait(1500);
const d3 = await wait(1500);
console.log(d1, d2, d3);
}
waitOnThings();
const BASE_URL = "https://pokeapi.co/api/v2";
fetch(`${BASE_URL}/pokemon/ditto`).then((response) => {
if (!response.ok)
throw "ERROR";
return response.json()
}).then(data => {
console.log(data.name)
})
async function getDitto() {
const response = await fetch(`${BASE_URL}/pokemon/ditto`);
if (!response.ok)
throw "ERROR";
const data = await response.json();
console.log(data.name);
}
getDitto()
async function getPokemon(list = ["ditto", "squirtle", "grotle", "houndour"]) {
const response0 = await fetch(`${BASE_URL}/pokemon/${list[0]}`)
const response1 = await fetch(`${BASE_URL}/pokemon/${list[1]}`)
const response2 = await fetch(`${BASE_URL}/pokemon/${list[2]}`)
const response3 = await fetch(`${BASE_URL}/pokemon/${list[3]}`)
const data0 = await response0.json();
const data1 = await response1.json();
const data2 = await response2.json();
const data3 = await response3.json();
console.log(data0, data1, data2, data3);
}
getPokemon()
async function getPokemonAtOnce(list = ["ditto", "squirtle", "grotle", "houndour"]) {
const responses = await Promise.all([
fetch(`${BASE_URL}/pokemon/${list[0]}`),
fetch(`${BASE_URL}/pokemon/${list[1]}`),
fetch(`${BASE_URL}/pokemon/${list[2]}`),
fetch(`${BASE_URL}/pokemon/${list[3]}`),
]);
const dataSet = await Promise.all(responses.map(r=>r.json()));
console.log(dataSet);
dataSet.forEach(p=>{
document.body.innerHTML += `<img src="${p.sprites.front_default}"/>`+p.name + "</br>";
})
//const data0 = await response0.json();
//const data1 = await response1.json();
//const data2 = await response2.json();
//const data3 = await response3.json();
//console.log(data0, data1, data2, data3);
}
getPokemonAtOnce();
// Weather API
const API_KEY = "<NEED-API-KEY>";
const API_URL = `http://api.openweathermap.org/data/2.5`
async function getForcast(city="memphis"){
const response = await fetch(`${API_URL}/weather?q=${city}&appid=${API_KEY}`);
const data = await response.json();
console.log(data);
}
getForcast();
///forecast?q={city name}&appid={API key}`