Idempotency keys
Implement idempotent(handler), the logic behind an Idempotency-Key header. handler(input) is async. idempotent returns a function (key, input) that returns a promise. The first call with a key runs handler(input). A call with the same key while that work is in flight gets the same result, and so does a call after it succeeded, without running the handler again. A failure is not remembered: callers waiting on it get the rejection, and the next call with that key runs the handler again. Any string can be a key. The answer has each outcome and the times the handler started. In the tests a task [30, a] resolves with a after 30 ms and [30, boom, true] rejects with Error(boom); an outcome is [status, value, time].
Start from
function idempotent(handler) {
// Return (key, input) => a promise, running handler once per key.
}Hints and solution
The app has this lab in JavaScript, TypeScript and Python. This page shows the JavaScript code.
Solve it in the app: its tests run without a year.
The hints, the solution and what an interviewer looks for opens with a year in the app. What a year includes