forked from jonasschmedtmann/complete-javascript-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasynchronous.html
136 lines (118 loc) · 4.4 KB
/
asynchronous.html
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
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Asynchronous JavaScript</title>
</head>
<body>
<h1>Asynchronous JavaScript</h1>
<script>
// const second = () => {
// setTimeout(() => {
// console.log('Async Hey there');
// }, 2000);
// }
// const first = () => {
// console.log('Hey there');
// second();
// console.log('The end');
// }
// first();
// function getRecipe() {
// setTimeout(() => {
// const recipeID = [523, 883, 432, 974];
// console.log(recipeID);
// setTimeout(id => {
// const recipe = {title: 'Fresh tomato pasta', publisher: 'Jonas'};
// console.log(`${id}: ${recipe.title}`);
// setTimeout(publisher => {
// const recipe2 = {title: 'Italian Pizza', publisher: 'Jonas'};
// console.log(recipe);
// }, 1500, recipe.publisher);
// }, 1500, recipeID[2]);
// }, 1500);
// }
// getRecipe();
// const getIDs = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve([523, 883, 432, 974]);
// }, 1500);
// });
// const getRecipe = recID => {
// return new Promise((resolve, reject) => {
// setTimeout(ID => {
// const recipe = {title: 'Fresh tomato pasta', publisher: 'Jonas'};
// resolve(`${ID}: ${recipe.title}`);
// }, 1500, recID);
// });
// };
// const getRelated = publisher => {
// return new Promise((resolve, reject) => {
// setTimeout(pub => {
// const recipe = {title: 'Italian Pizza', publisher: 'Jonas'};
// resolve(`${pub}: ${recipe.title}`);
// }, 1500, publisher);
// });
// };
// getIDs
// .then(IDs => {
// console.log(IDs);
// return getRecipe(IDs[2]);
// })
// .then(recipe => {
// console.log(recipe);
// return getRelated('Jonas Schmedtmann');
// })
// .then(recipe => {
// console.log(recipe);
// })
// .catch(error => {
// console.log('Error!!');
// });
// async function getRecipesAW() {
// const IDs = await getIDs;
// console.log(IDs);
// const recipe = await getRecipe(IDs[2]);
// console.log(recipe);
// const related = await getRelated('Jonas Schmedtmann');
// console.log(related);
// return recipe;
// }
// getRecipesAW().then(result => console.log(`${result} is the best ever!`));
function getWeather(woeid) {
fetch(`https://crossorigin.me/https://www.metaweather.com/api/location/${woeid}/`)
.then(result => {
// console.log(result);
return result.json();
})
.then(data => {
// console.log(data);
const today = data.consolidated_weather[0];
console.log(`Temperatures today in ${data.title} stay between ${today.min_temp} and ${today.max_temp}.`);
})
.catch(error => console.log(error));
}
getWeather(2487956);
getWeather(44418);
async function getWeatherAW(woeid) {
try {
const result = await fetch(`https://crossorigin.me/https://www.metaweather.com/api/location/${woeid}/`);
const data = await result.json();
const tomorrow = data.consolidated_weather[1];
console.log(`Temperatures tomorrow in ${data.title} stay between ${tomorrow.min_temp} and ${tomorrow.max_temp}.`);
return data;
} catch(error) {
alert(error);
}
}
getWeatherAW(2487956);
let dataLondon;
getWeatherAW(44418).then(data => {
dataLondon = data
console.log(dataLondon);
});
</script>
</body>
</html>