shared-helper-two-layers.ts 764 B

1234567891011121314
  1. // Two wrapper layers over one callback: `retry` hands `limited` a fresh arrow that calls
  2. // retry's own parameter. The arrow is bound at retry's call to limited; `fn()` inside it
  3. // resolves through the enclosing activation of retry, so each caller's chain inlines
  4. // retry → limited → arrow → its own callback: 4 call regions per caller, no choice.
  5. async function limited(fn: () => Promise<string>): Promise<string> {
  6. return await fn();
  7. }
  8. async function retry(fn: () => Promise<string>): Promise<string> {
  9. return await limited(() => fn());
  10. }
  11. const a = await retry(async () => agent("a").ask<string>("a"));
  12. const b = await retry(async () => agent("b").ask<string>("b"));
  13. const c = await retry(async () => agent("c").ask<string>("c"));
  14. return [a, b, c];