From 8fe9c7fd2e537e91b8b7d7d541ea5b56833c2ed5 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 8 Apr 2023 23:40:56 -0400 Subject: [PATCH] * Add a function to check if an object is a Promise and a utility function to asynchronously get a property if it's a Promise or a function. --- src/index.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/index.ts b/src/index.ts index 95cac4e..4a82233 100644 --- a/src/index.ts +++ b/src/index.ts @@ -162,3 +162,23 @@ export const factory = function ( }); }; }; + +export async function maybeGetAsyncProperty(object: any) { + if (typeof object === "function") { + object = object(); + } + + if (isPromise(object)) { + object = await object; + } + + return object; +} + +export function isPromise(obj: Promise) { + return ( + !!obj && + (typeof obj === "object" || typeof obj === "function") && + typeof obj.then === "function" + ); +}