/
8.async-quiz.js
50 lines (40 loc) · 965 Bytes
/
8.async-quiz.js
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
function fetchEgg(chicken) {
return Promise.resolve(`${chicken} => Egg`);
}
function fryEgg(egg) {
return Promise.resolve(`${egg} => fry`);
}
function getChicken() {
return Promise.resolve(`get Chicken`);
}
function getChickenError() {
return Promise.reject(new Error(`not found checken`));
}
fetchEgg('chicken').then((egg) => console.log(egg));
console.clear();
getChicken()
.then((chicken) => {
return fetchEgg(chicken);
})
.then((egg) => fryEgg(egg))
.then((friedEgg) => console.log(friedEgg));
async function fecthGetChicken() {
const chicken = await getChicken();
const egg = await fetchEgg();
return fryEgg(egg);
}
fecthGetChicken().then(console.log);
console.log('dd');
console.clear();
// Solution
async function makeFriedEgg() {
let chicken;
try {
chicken = await getChicken();
} catch {
chicken = 'egg';
}
const egg = await fetchEgg(chicken);
return fryEgg(egg);
}
makeFriedEgg().then(console.log);