-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncronous.js
More file actions
171 lines (129 loc) · 4.36 KB
/
asyncronous.js
File metadata and controls
171 lines (129 loc) · 4.36 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//ASYNCRONOUS JAVASCRIPT
(()=>{
const second = () => {
setTimeout(() => {
console.log('delay')
}, 3000);
}
const first = () => {
console.log('First');
second();
console.log('After')
}
first();
});
(() => {
//CALLBACK HELL
//PROMISES ARE USED TO AVOID PROMISES
function getRecipes(){
setTimeout(() => {
const recipeIDs = [23, 54, 65, 23, 45];
console.log(recipeIDs);
setTimeout(id => {
const recipe = {
recipeName: 'Making Pasta',
author: 'Eric'
}
console.log(`Recipe ${id} is ${recipe.recipeName} made by ${recipe.author}`);
setTimeout(publisher => {
const recipe2 = {
recipeName: 'Pizza',
author: 'Eric'
}
console.log(recipe2);
}, 1500, recipe.author);
}, 1000, recipeIDs[3])
}, 1500);
}
getRecipes();
});
//PROMISES -- Keeps track of whether an event happened
(() => {
const getIDs = new Promise((resolve, reject) => {
setTimeout(() => {
// reject('Missed some data'); //Resolve and reject act as kind of RETURNs because program execution ends immidiately one of them is executed
resolve([23, 54, 65, 23, 45]);
}, 1500);
});
const getRecipe = recipeID => {
return new Promise((resolve, reject) =>{
setTimeout(id => {
const recipe = {
recipeName: 'Making Pasta',
author: 'Eric'
}
resolve(`Recipe ID ${id} is ${recipe.recipeName} made by ${recipe.author}`);
}, 1500, recipeID)
});
}
const recipe2 = publisher => {
return new Promise((resolve, reject) => {
setTimeout(id => {
const recipe2Obj = {
recipeName: 'Pizza',
}
console.log(`2nd recipe: ${recipe2Obj.recipeName}`);
}, 1500, publisher)
});
}
//Consuming the promises -- in ES6 (ES2015)
/*
getIDs.then(IDs => {
console.log(IDs);
return getRecipe(IDs[2]);
})
.then(recipe => {
console.log(recipe);
return recipe2(recipe.author)
})
.catch(error => {
console.log('ERROR!!')
});
*/
//ASYNC AWAIT -- Consuming promises in a better way -- ES8 (ES2017)
async function getRecipeAsync(){
const IDs = await getIDs;
// console.log(IDs);
const recipe = await getRecipe(IDs[4]);
console.log(recipe);
const recipeTwo = await recipe2(recipe.author);
console.log(recipeTwo);
return recipe;
}
getRecipeAsync().then(res => console.log(`ID: ${res}`));
});
//AJAX REQUESTS AND APIs -- Using METAWEATHER and CrossOrigin
(() =>{
const getWeather = (woeid) => {
fetch(`https://crossorigin.me/https://www.metaweather.com/api/location/${woeid}/`).then(result => {
console.log(result);
return result.json();
})
.then(data => {
const today = data.consolidated_weather[0];
console.log(`Temperatures in ${data.title} stay between ${today.min_Temp} and ${today.max_temp}`);
})
.catch(error => {
console.log(error)
});
}
// getWeather(2487956);
// getWeather(44418);
//USING ASYNC AWAIT function
async function getWeatherAW(woeid){
try {
const result = await fetch(`https://crossorigin.me/https://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/${woeid}/`);
const data = await result.json();
const today = data.consolidated_weather[0];
console.log(`Temperatures in ${data.title} stay between ${today.min_Temp} and ${today.max_temp}`);
return data;
} catch(error){
console.log(error);
}
}
getWeatherAW(2487956);
getWeatherAW(44418).then(data => {
const london = data;
console.log(london);
});
});