From cbd8301646b40935779ca6975f22c0fe981a32ae Mon Sep 17 00:00:00 2001 From: Randolf Jung Date: Tue, 1 Sep 2026 08:39:21 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Accept=20PromiseLike=20values=20?= =?UTF-8?q?in=20call()=20and=20until()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/call.ts | 16 ++++++++-------- lib/until.ts | 4 ++-- test/call.test.ts | 5 +++++ test/until.test.ts | 10 ++++++++++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/call.ts b/lib/call.ts index 84776a1f5..eaecef951 100644 --- a/lib/call.ts +++ b/lib/call.ts @@ -27,7 +27,7 @@ import type { Operation } from "./types.ts"; * @since 3.0 */ export interface Callable< - T extends Operation | Promise | unknown, + T extends Operation | PromiseLike | unknown, TArgs extends unknown[] = [], > { (...args: TArgs): T; @@ -69,7 +69,7 @@ export interface Callable< * @since 3.0 */ export function call( - fn: (...args: TArgs) => Promise, + fn: (...args: TArgs) => PromiseLike, ): Operation; export function call( fn: (...args: TArgs) => Operation, @@ -90,7 +90,7 @@ export function call( target instanceof Map || target instanceof Set ) { return constant(target)[Symbol.iterator](); - } else if (isPromise(target)) { + } else if (isPromiseLike(target)) { return action(function wait(resolve, reject) { target.then(resolve, reject); return () => {}; @@ -104,14 +104,14 @@ export function call( }; } -function isPromise( - target: Operation | Promise | T, -): target is Promise { - return target && typeof (target as Promise).then === "function"; +function isPromiseLike( + target: Operation | PromiseLike | T, +): target is PromiseLike { + return target && typeof (target as PromiseLike).then === "function"; } function isOperation( - target: Operation | Promise | T, + target: Operation | PromiseLike | T, ): target is Operation { return target && typeof (target as Operation)[Symbol.iterator] === "function"; diff --git a/lib/until.ts b/lib/until.ts index bb4be40cf..e9caab387 100644 --- a/lib/until.ts +++ b/lib/until.ts @@ -13,9 +13,9 @@ import type { Operation } from "./types.ts"; * @returns {Operation} that succeeds or fails depending on the outcome of `promise` * @since 3.4 */ -export function until(promise: Promise): Operation { +export function until(promise: PromiseLike): Operation { return action((resolve, reject) => { - promise.then(resolve).catch(reject); + promise.then(resolve, reject); return () => {}; }); } diff --git a/test/call.test.ts b/test/call.test.ts index 19229097a..9c3bd182d 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -28,6 +28,11 @@ describe("call", () => { ); }); + it("evaluates a promise-like function", async () => { + let promiseLike: PromiseLike = Promise.resolve(42); + await expect(run(() => call(() => promiseLike))).resolves.toEqual(42); + }); + it("evaluates a vanilla function", async () => { await expect(run(() => call(() => 42))).resolves.toEqual(42); }); diff --git a/test/until.test.ts b/test/until.test.ts index bdf5600e6..5773afbe5 100644 --- a/test/until.test.ts +++ b/test/until.test.ts @@ -9,6 +9,16 @@ describe("until", () => { expect(yield* until(Promise.resolve(42))).toEqual(42); }); }); + it("resolves promise-like values", async () => { + let promiseLike = { + then(resolve: (value: number) => void) { + resolve(42); + return promiseLike; + }, + } as PromiseLike; + + await expect(run(() => until(promiseLike))).resolves.toEqual(42); + }); it("preserves non-Error promise rejections", async () => { let cause = { message: "error" };