-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoize.js
37 lines (28 loc) · 906 Bytes
/
memoize.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
function memoize(method) {
let cache = {};
return async function() {
let args = JSON.stringify(arguments);
cache[args] = cache[args] || method.apply(this, arguments);
return cache[args];
};
}
let getSoupRecipe = memoize(async function (soupType) {
return await http.get(`/api/soup/${soupType}`);
});
let buySoupPan = memoize(async function () {
return await http.get(`/api/soupPan`);
});
let hireSoupChef = memoize(async function (soupType) {
let soupRecipe = await getSoupRecipe(soupType)
return await http.post(`/api/soupChef/hire`, {
requiredSkills: soupRecipe.requiredSkills
});
});
let makeSoup = memoize(async function (soupType) {
let [soupRecipe, soupPan, soupChef] = await Promise.all([
getSoupRecipe(soupType), buySoupPan(), hireSoupChef(soupType)
]);
return await http.post(`api/makeSoup`, {
soupRecipe, soupPan, soupChef
});
});