diff --git a/src/concurrency/lazy-thenable.spec.ts b/src/concurrency/lazy-thenable.spec.ts index 41d6463..6d05f47 100644 --- a/src/concurrency/lazy-thenable.spec.ts +++ b/src/concurrency/lazy-thenable.spec.ts @@ -7,10 +7,13 @@ describe(lazyThenable, () => { const lazy1 = lazyThenable(async () => ++called); expect(called).toEqual(0); + expect(lazy1.executed).toBe(false); const converted = Promise.resolve(lazy1); expect(called).toEqual(0); + expect(lazy1.executed).toBe(false); await wait(0); + expect(lazy1.executed).toBe(true); expect(called).toEqual(1); expect(await converted).toEqual(1); expect(await lazy1).toEqual(1); diff --git a/src/concurrency/lazy-thenable.ts b/src/concurrency/lazy-thenable.ts index d1954ed..6e1912a 100644 --- a/src/concurrency/lazy-thenable.ts +++ b/src/concurrency/lazy-thenable.ts @@ -3,14 +3,18 @@ * @param io * @return */ -export function lazyThenable(io: () => T): PromiseLike> { +export function lazyThenable(io: () => T): LazyThenable> { let r: null | Promise> = null; return { - then( - onfulfilled?: ((value: Awaited) => PromiseLike | TResult1) | undefined | null, - onrejected?: ((reason: any) => PromiseLike | TResult2) | undefined | null, - ): PromiseLike { + get executed(): boolean { + return r !== null; + }, + then(onfulfilled, onrejected): PromiseLike { return (r ??= Promise.resolve(io())).then(onfulfilled, onrejected); }, }; } + +interface LazyThenable extends PromiseLike { + get executed(): boolean; +}