Circuit breaker
Implement createBreaker(call, { failureThreshold, cooldownMs }). It returns a function that passes its arguments to call, an async function, and returns its promise, while the circuit is closed. After failureThreshold failures in a row the circuit opens: calls then reject with new Error('circuit open') without calling call. Once cooldownMs have passed since it opened, the next call is a trial and goes through; success closes the circuit and resets the count, failure opens it again for a new cooldown. A success while closed also resets the count. Read the time with Date.now(). In the tests call resolves with ok or rejects with Error(down); the answer has each outcome, [status, value, time], and the times call was reached.
Start from
function createBreaker(call, { failureThreshold, cooldownMs }) {
// Return a function that calls call, or fails fast while open.
}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