Last active
April 22, 2021 02:43
-
-
Save ericdmoore/4191d5a8a21f8e8825a5b942ebf60338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vanilla version of TS (from esbiuild) | |
const {isArray} = Array; | |
const isPrim = (i) => { | |
const types = ["string", "number", "boolean", "function", "undefined", "bigint", "symbol"]; | |
return types.some((tStr) => typeof i === tStr); | |
}; | |
const isObject = (i) => { | |
return !isPrim(i) && !isArray(i); | |
}; | |
export const PromiseResolve = async (promisedInputObj) => { | |
const resolvedInput = await promisedInputObj; | |
const keys = Object.keys(resolvedInput); | |
const vals = await Promise.all(Object.values(j)); | |
return vals.reduce(async (prior, val, i) => ({ | |
...await prior, | |
[keys[i]]: isObject(val) ? await PromiseResolve(val) : val | |
}), Promise.resolve({})); | |
}; | |
export default PromiseResolve; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #region interfaces | |
type PromiseOr<T> = Promise<T> | T | |
type DictUnknowns = {[str:string]:unknown} | |
// #endregion interfaces | |
const { isArray } = Array | |
const isPrim = (i:unknown) => { | |
const types = ['string', 'number', 'boolean', 'function', 'undefined', 'bigint', 'symbol'] | |
// eslint-disable-next-line valid-typeof | |
return types.some(tStr => typeof i === tStr) | |
} | |
const isObject = (i:unknown) : i is {[s:string]:unknown} => { | |
return !isPrim(i) && !isArray(i) | |
} | |
/** | |
* | |
* @param promisedInputObj | |
*/ | |
export const PromiseResolve = async (promisedInputObj: PromiseOr<DictUnknowns>): Promise<DictUnknowns> => { | |
const resolvedInput = await promisedInputObj | |
const keys = Object.keys(resolvedInput) | |
const vals = await Promise.all(Object.values(resolvedInput)) | |
return vals.reduce(async (prior:Promise<DictUnknowns>, val, i) => ({ | |
...(await prior), | |
[keys[i]]: isObject(val) | |
? await PromiseResolve(val as PromiseOr<DictUnknowns>) | |
: val | |
}), Promise.resolve({}) as Promise<DictUnknowns>) | |
} | |
export default PromiseResolve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wish :
Promise.resolve({}, {recursive: true})
is not Array && is not Primitive
- maybe there's a way and I just need to ask someone smarter.