Last active
April 19, 2022 12:43
-
-
Save voxpelli/eb0bc203c91e647bfc88769bf55c6a97 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
// Used in real world eg. here: https://github.com/voxpelli/node-format-microformat/blob/5381268dbcdb1aef6a5757758710e4b9f75cbea3/index.js#L72-L78 | |
// Works | |
/** @typedef {null|undefined|boolean|string|number|Array} NonObject */ | |
/** | |
* @template T | |
* @typedef {T|Promise<T>} MaybePromised | |
/** | |
* Resolves a value in a way so that it can be set either statically, dynamically or even asyncronously | |
* | |
* @template {NonObject} T | |
* @template {any[]} P | |
* @param {((...params: P) => MaybePromised<T>)|MaybePromised<T>} value | |
* @param {P} valueArgs | |
* @returns {Promise<T>} | |
*/ | |
const resolveNonObjectValue = (value, ...valueArgs) => resolveValue(value, ...valueArgs); | |
// But preferably wouldn't want to limit myself to non-objects, but getting issues in Typescript thinking that `value` isn't always a callable when trying to do so | |
const resolveValue = async function (value, ...valueArgs) { | |
if (typeof value === 'function') { | |
value = value.apply(undefined, valueArgs); | |
} | |
return value; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment