-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (48 loc) · 1.28 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
// Examples for the `withCache` function wrapper.
const cache = require('./function-cache');
/* Imagine this is a very slow API call
* or complex calculation */
function slowAPICall(name) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Hello, ${name}!`);
}, 10000);
});
}
async function slowFunction(name) {
if (!name) {
return "Please provide a valid name.";
}
return slowAPICall(name);
}
/* Imagine this is another very slow API call
* or complex calculation */
function slowAdditionPromise(x, y, z) {
const result = x + y + z;
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(result);
}, 10000);
});
}
async function slowAddition(x, y, z) {
return slowAdditionPromise(x, y, z);
}
// Example 1: calling a function with one parameter
function example1() {
const cachedFunction = cache.withCache('SLOW_FUNCTION', slowFunction);
cachedFunction('Jacob')
.then((greeting) => {
console.log(greeting);
})
}
// Example 2: calling a function with multiple parameters
function example2() {
const anotherCachedFunction = cache.withCache('ADDITION', slowAddition);
anotherCachedFunction(1, 2, 3)
.then((result) => {
console.log(result);
})
}
// example1();
example2();