Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if awaitable is an object #43

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function executeAsync<T> (function_: AsyncFunction<T>): [Promise<T>, () =
}
};
let awaitable = function_();
if ("catch" in awaitable) {
if (typeof awaitable === "object" && "catch" in awaitable) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
awaitable = awaitable.catch((error) => {
restore();
throw error;
Expand Down
13 changes: 13 additions & 0 deletions test/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it, expect, vi } from "vitest";
import { createContext, withAsyncContext } from "../src";

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const noop = () => {};

describe("callAsync", () => {
it("call and use", async () => {
Expand Down Expand Up @@ -86,4 +87,16 @@ describe("callAsync", () => {
// eslint-disable-next-line no-console
console.warn = _warn;
});

it("await but no async fn", async () => {
const context = createContext();
await context.callAsync("A", async () => {
expect(context.use()).toBe("A");
await noop();
expect(context.use()).toBe("A");
await "A";
expect(context.use()).toBe("A");
return context.use();
});
});
});