* 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:
parent
00f4e4f3ad
commit
8fe9c7fd2e
20
src/index.ts
20
src/index.ts
|
@ -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"
|
||||
);
|
||||
}
|
||||
|
|
Reference in New Issue