Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { Operation } from "./types.ts";
* @since 3.0
*/
export interface Callable<
T extends Operation<unknown> | Promise<unknown> | unknown,
T extends Operation<unknown> | PromiseLike<unknown> | unknown,
TArgs extends unknown[] = [],
> {
(...args: TArgs): T;
Expand Down Expand Up @@ -69,7 +69,7 @@ export interface Callable<
* @since 3.0
*/
export function call<T, TArgs extends unknown[] = []>(
fn: (...args: TArgs) => Promise<T>,
fn: (...args: TArgs) => PromiseLike<T>,
): Operation<T>;
export function call<T, TArgs extends unknown[] = []>(
fn: (...args: TArgs) => Operation<T>,
Expand All @@ -90,7 +90,7 @@ export function call<T, TArgs extends unknown[] = []>(
target instanceof Map || target instanceof Set
) {
return constant(target)[Symbol.iterator]();
} else if (isPromise<T>(target)) {
} else if (isPromiseLike<T>(target)) {
return action<T>(function wait(resolve, reject) {
target.then(resolve, reject);
return () => {};
Expand All @@ -104,14 +104,14 @@ export function call<T, TArgs extends unknown[] = []>(
};
}

function isPromise<T>(
target: Operation<T> | Promise<T> | T,
): target is Promise<T> {
return target && typeof (target as Promise<T>).then === "function";
function isPromiseLike<T>(
target: Operation<T> | PromiseLike<T> | T,
): target is PromiseLike<T> {
return target && typeof (target as PromiseLike<T>).then === "function";
}

function isOperation<T>(
target: Operation<T> | Promise<T> | T,
target: Operation<T> | PromiseLike<T> | T,
): target is Operation<T> {
return target &&
typeof (target as Operation<T>)[Symbol.iterator] === "function";
Expand Down
4 changes: 2 additions & 2 deletions lib/until.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import type { Operation } from "./types.ts";
* @returns {Operation<T>} that succeeds or fails depending on the outcome of `promise`
* @since 3.4
*/
export function until<T>(promise: Promise<T>): Operation<T> {
export function until<T>(promise: PromiseLike<T>): Operation<T> {
return action((resolve, reject) => {
promise.then(resolve).catch(reject);
promise.then(resolve, reject);
return () => {};
});
}
5 changes: 5 additions & 0 deletions test/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ describe("call", () => {
);
});

it("evaluates a promise-like function", async () => {
let promiseLike: PromiseLike<number> = 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);
});
Expand Down
10 changes: 10 additions & 0 deletions test/until.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>;

await expect(run(() => until(promiseLike))).resolves.toEqual(42);
});
it("preserves non-Error promise rejections", async () => {
let cause = { message: "error" };

Expand Down
Loading