fix(kilo-sessions): handle sync throws in inflight cache
Wrap callback execution in a promise microtask so synchronous errors flow through the existing `.catch()` and the in-flight entry is reliably cleaned up. Update tests to await the microtask before asserting call count.
This commit is contained in:
@@ -37,7 +37,9 @@ export function withInFlightCache<T>(
|
||||
inflight: undefined,
|
||||
}
|
||||
|
||||
const task = cb()
|
||||
// Guard against synchronous throws in `cb()` by forcing it into the promise chain.
|
||||
// This ensures errors flow through the `.catch()` below and the cache entry is cleaned up.
|
||||
const task = Promise.resolve().then(() => cb())
|
||||
.then((value) => {
|
||||
if (value === undefined) {
|
||||
// `undefined` is treated as a non-cacheable sentinel.
|
||||
|
||||
@@ -39,6 +39,9 @@ describe("withInFlightCache", () => {
|
||||
const b = withInFlightCache(key, 10_000, cb)
|
||||
|
||||
expect(a).toBe(b)
|
||||
// cb() is invoked via a microtask in withInFlightCache().
|
||||
// Allow it to run before asserting call count.
|
||||
await Promise.resolve()
|
||||
expect(calls.count).toBe(1)
|
||||
|
||||
job.resolve(123)
|
||||
|
||||
Reference in New Issue
Block a user