forked from meandmax/redux-loop-ts-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
29 lines (25 loc) · 819 Bytes
/
api.ts
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
// Simulate a flaky API around otherwise an otherwise synchronous `f()`.
const flakify = <T>(f: () => T): Promise<T> =>
new Promise((resolve, reject) =>
// We'll always take 200 * (1d10 + 1) ms to respond
window.setTimeout(() => {
try {
// And ~20% of the time we'll fail
if (Math.random() < 0.2) {
throw new Error("Failed arbitrarily")
}
resolve(f())
} catch (e) {
return reject(e)
}
}, 200 + Math.random() * 2000)
)
const SAVE_LOCATION = "__counterValue"
export const save = (counter: number): Promise<void> =>
flakify(() => {
localStorage.setItem(SAVE_LOCATION, String(counter))
})
export const load = (): Promise<number> =>
flakify(() => {
return parseInt(localStorage.getItem(SAVE_LOCATION) || "", 10) || 0
})