Repository navigation
Expand file tree
/
Copy pathThrottle 04.js
More file actions
126 lines (102 loc) · 2.94 KB
/
Copy pathThrottle 04.js
File metadata and controls
126 lines (102 loc) · 2.94 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
/*🔥 Advanced Challenge: API Throttle with Success/Error Handling
You are given:
function fakeAPI(userId) {
console.log(`Request started for user ${userId}`);
return new Promise((resolve, reject) => {
setTimeout(() => {
if (userId === 3) {
reject(new Error("Server error"));
} else {
resolve(`User ${userId} loaded`);
}
}, 1000);
});
}
Build this from scratch:
function throttleAsync(fn, delay) {
// your code
}
Requirements
Create:
const loadUser = throttleAsync(fakeAPI, 2000);
Then:
loadUser(1);
loadUser(2);
setTimeout(() => {
loadUser(3);
}, 500);
setTimeout(() => {
loadUser(4);
}, 1000);
setTimeout(() => {
loadUser(5);
}, 2500);
Your throttle must:
Execute the first call immediately.
While throttled, keep only the latest pending call.
After 2 seconds, execute that latest pending call.
Start another 2-second throttle period.
Preserve any number of arguments using ...args.
Correctly handle the Promise returned by fn.
If fn rejects, the throttle itself must not break. It should continue accepting future calls.
Return the Promise from the actual API call so that this works:
loadUser(1)
.then(result => console.log(result))
.catch(error => console.log(error.message));
🧠 The tricky part
Think carefully about this:
fn(...args)
returns a Promise.
What should your throttle do with that Promise?
And if:
fn(...args)
rejects, should waiting remain stuck at true?*/
//GIVEN...
function fakeAPI(userId) {
console.log(`Request started for user ${userId}`);
return new Promise((resolve, reject) => {
setTimeout(() => {
if (userId === 3) {
reject(new Error("Server error"));
} else {
resolve(`User ${userId} loaded`);
}
}, 1000);
});
}
//SOLUTION
function throttleAsync(fn, delay) {
let waiting = false;
let storage = null;
return function (...args) {
if (waiting === true) {
storage = args;
return;
}
const promise = fn(...args)
.then((response) => {
console.log(response);
return response; //had to put this so as to be able to return the promise when it is resolved successful
})
.catch((error) => {
console.error(error.message);
throw error; //had to put this so as to rethrow the error so that it doesn't get to be resolved.
});
waiting = true;
setTimeout(() => {
waiting = false;
if (storage !== null) {
let argsToRun = storage;
storage = null;
fn(...argsToRun) //Here our function does not return promise for those calls that occur later when our function has finished.
.then((response) => console.log(response))
.catch((error) => console.error(error.message));
waiting = true;
setTimeout(() => (waiting = false), delay);
}
}, delay);
return promise;
};
}
const loadUser = throttleAsync(fakeAPI, 2000);
loadUser(2).catch((error) => console.log("Error caught:", error.message));