diff --git a/src/index.ts b/src/index.ts index 97dd33c..45cd442 100644 --- a/src/index.ts +++ b/src/index.ts @@ -148,7 +148,7 @@ export function executeAsync (function_: AsyncFunction): [Promise, () = } }; let awaitable = function_(); - if ("catch" in awaitable) { + if (awaitable && typeof awaitable === "object" && "catch" in awaitable) { awaitable = awaitable.catch((error) => { restore(); throw error; diff --git a/test/async.test.ts b/test/async.test.ts index cc5ed39..a4a2bf5 100644 --- a/test/async.test.ts +++ b/test/async.test.ts @@ -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 () => { @@ -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(); + }); + }); });