From 4ee5f84793f7fb92d238f2e1923e15068724bff4 Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Sun, 27 Nov 2022 21:04:11 +0100 Subject: [PATCH 1/2] fix: check if awaitable is an object --- src/index.ts | 2 +- test/async.test.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 97dd33c..a2c5026 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 (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(); + }); + }); }); From 392cde69b59159707bab4f766b69a7c0f42bc636 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Mon, 28 Nov 2022 12:35:05 +0100 Subject: [PATCH 2/2] Update src/index.ts --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index a2c5026..45cd442 100644 --- a/src/index.ts +++ b/src/index.ts @@ -148,7 +148,7 @@ export function executeAsync (function_: AsyncFunction): [Promise, () = } }; let awaitable = function_(); - if (typeof awaitable === "object" && "catch" in awaitable) { + if (awaitable && typeof awaitable === "object" && "catch" in awaitable) { awaitable = awaitable.catch((error) => { restore(); throw error;