You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// In ES6 to consume promises syntax can still be quite confusing and difficult to manage.And, so in ES8, or ES2017, something alled Async/Await was introduced to the JavaScript language.
2
+
3
+
constgetIDs=newPromise((resolve,reject)=>{
4
+
setTimeout(()=>{
5
+
resolve([523,883,432,974]);
6
+
},1500);
7
+
});
8
+
9
+
constgetRecipe=recID=>{
10
+
returnnewPromise((resolve,reject)=>{
11
+
setTimeout(ID=>{
12
+
13
+
constrecipe={
14
+
title: 'Fresh tomato Pasta',
15
+
Publisher: 'Lakshman'
16
+
};
17
+
18
+
resolve(`${ID}: ${recipe.title}`);
19
+
20
+
},1500,recID);
21
+
});
22
+
};
23
+
24
+
constgetRelated=Publisher=>{
25
+
returnnewPromise((resolve,reject)=>{
26
+
setTimeout(pub=>{
27
+
constrecipe={
28
+
title: 'American Pizza',
29
+
Publisher: pub
30
+
};
31
+
32
+
resolve(`${pub}: ${recipe.title}`);
33
+
34
+
},1500,Publisher);
35
+
});
36
+
};
37
+
38
+
/*
39
+
// this then method allows us to add an event handler for the case that the promise is fulfilled. So which means that there is a result.
40
+
getIDs
41
+
.then(IDs => { // so this argument here will be the result of the successful promise.
42
+
console.log(IDs); // [ 523, 883, 432, 974 ]
43
+
return getRecipe(IDs[2]);
44
+
})
45
+
46
+
.then(recipe => {
47
+
console.log(recipe); // 432: Fresh tomato Pasta
48
+
return getRelated('Lakshman Gope');
49
+
})
50
+
51
+
.then(recipe => {
52
+
console.log(recipe); // Lakshman Gope: American Pizza
53
+
54
+
})
55
+
// if the promise rejected then catch method will catch the error.
56
+
.catch(error => {
57
+
console.log('Error: ' + error);
58
+
}); */
59
+
60
+
61
+
// it's good to remember that async function always returns a promise.
62
+
asyncfunctiongetRecipeAW(){
63
+
constIDs=awaitgetIDs;
64
+
console.log(IDs);// [ 523, 883, 432, 974 ]
65
+
66
+
constrecipe=awaitgetRecipe(IDs[2]);
67
+
console.log(recipe);// 432: Fresh tomato Pasta
68
+
69
+
constrelated=awaitgetRelated('Lakshman Gope');
70
+
console.log(related);// Lakshman Gope: American Pizza
71
+
72
+
returnrecipe;
73
+
}
74
+
75
+
getRecipeAW().then(result=>console.log(`${result} is the best ever!`));// 432: Fresh tomato Pasta is the best ever!
0 commit comments