* 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.

This commit is contained in:
Derrick Hammer 2023-04-08 23:40:56 -04:00
parent 00f4e4f3ad
commit 8fe9c7fd2e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 20 additions and 0 deletions

View File

@ -162,3 +162,23 @@ export const factory = function <T extends Client = Client>(
});
};
};
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<any>) {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function"
);
}