diff --git a/src/common/utils/is-promise/is-promise.test.ts b/src/common/utils/is-promise/is-promise.test.ts new file mode 100644 index 0000000000..565f272ed6 --- /dev/null +++ b/src/common/utils/is-promise/is-promise.test.ts @@ -0,0 +1,31 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { isPromise } from "./is-promise"; + +describe("isPromise", () => { + it("given promise, returns true", () => { + const actual = isPromise(new Promise(() => {})); + + expect(actual).toBe(true); + }); + + it("given non-promise, returns false", () => { + const actual = isPromise({}); + + expect(actual).toBe(false); + }); + + it("given thenable, returns false", () => { + const actual = isPromise({ then: () => {} }); + + expect(actual).toBe(false); + }); + + it("given nothing, returns false", () => { + const actual = isPromise(undefined); + + expect(actual).toBe(false); + }); +}); diff --git a/src/common/utils/is-promise/is-promise.ts b/src/common/utils/is-promise/is-promise.ts new file mode 100644 index 0000000000..6261f569cd --- /dev/null +++ b/src/common/utils/is-promise/is-promise.ts @@ -0,0 +1,7 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +export function isPromise(reference: any): reference is Promise { + return reference?.constructor === Promise; +} diff --git a/src/common/utils/with-error-logging/with-error-logging.injectable.ts b/src/common/utils/with-error-logging/with-error-logging.injectable.ts index 5b9f3d5b21..12b48c6204 100644 --- a/src/common/utils/with-error-logging/with-error-logging.injectable.ts +++ b/src/common/utils/with-error-logging/with-error-logging.injectable.ts @@ -4,6 +4,7 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import loggerInjectable from "../../logger.injectable"; +import { isPromise } from "../is-promise/is-promise"; export type WithErrorLoggingFor = ( getErrorMessage: (error: unknown) => string @@ -44,7 +45,3 @@ const withErrorLoggingInjectable = getInjectable({ }); export default withErrorLoggingInjectable; - -function isPromise(reference: any): reference is Promise { - return !!reference?.then; -}