toolbox
toolbox copied to clipboard
fix(once): correct error handling to not cache thrown errors
The once function was caching error states - when the wrapped function threw on first invocation, subsequent calls returned undefined instead of retrying. This differs from standard once semantics where only successful results are cached.
Changes
-
Reordered execution flag assignment - Move
called = trueafterfn.apply()so errors prevent caching -
Updated error handling test - Test now verifies function retries until success rather than returning
undefined
Behavior
const initialize = once(() => {
if (!isReady()) throw new Error('Not ready');
return setup();
});
// Before: First call throws, second call returns undefined
// After: Retries until isReady() returns true, then caches result
initialize(); // throws
initialize(); // retries (may throw or succeed)
initialize(); // returns cached result if previous call succeeded
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.