Skip to content

Conversation

Copy link

Copilot AI commented Dec 19, 2025

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 = true after fn.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.

Co-authored-by: ASafaeirad <28571761+ASafaeirad@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement once function with documentation and tests fix(once): correct error handling to not cache thrown errors Dec 19, 2025
Copilot AI requested a review from ASafaeirad December 19, 2025 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants