Promisify a Callback Function
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6 — Pratik Rai ·
Cache a function by its arguments so the same call never computes twice.
Write memoize(fn) returning a function that behaves identically but never recomputes for arguments it has already seen.
The returned function also exposes getCallCount(), reporting how many times the original actually ran.
Input:
JSfile.javascript1const sum = memoize((a, b) => a + b); 2sum(2, 2); // 4, computed 3sum(2, 2); // 4, from cache 4sum.getCallCount();
Output:
1
The second call was served from the cache, so the original ran once.
0, false or undefined is still a cached value — check whether the key exists, not whether the value is truthy.Goal: Serve repeat arguments from cache, and report the real call count.
Continue learning with these related challenges
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6 — Pratik Rai ·
A once wrapper: the first call runs, every call after it does nothing.
JavaScript · ES6 — Pratik Rai ·
sum(1)(2)(3) with no fixed arity — the running total appears when the result is coerced.
JavaScript · ES6 — Pratik Rai ·
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6
Pratik Rai ·
A once wrapper: the first call runs, every call after it does nothing.
JavaScript · ES6
Pratik Rai ·
sum(1)(2)(3) with no fixed arity — the running total appears when the result is coerced.
JavaScript · ES6
Pratik Rai ·