Added sync function runner
All checks were successful
Build / Build NPM Project (push) Successful in 42s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 41s

This commit is contained in:
Zakary Timson 2025-05-06 19:52:32 -04:00
parent 2e4559d805
commit 3fd5c5ed57
2 changed files with 5 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.24.1",
"version": "0.24.2",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -5,11 +5,12 @@ import {md5} from './string';
* Run a stringified function with arguments asynchronously
* @param {object} args Map of key/value arguments
* @param {string} fn Function as string
* @return {Promise<T>} Function string response
* @param {boolean} async Run with async (returns a promise)
* @return {T | Promise<T>} Function return result
*/
export function asyncFunction<T>(args: object, fn: string): Promise<T> {
export function fn<T>(args: object, fn: string, async: boolean = false): T {
const keys = Object.keys(args);
return new Function(...keys, `return (async (${keys.join(',')}) => { ${fn} })(${keys.join(',')})`)(...keys.map(k => (<any>args)[k]));
return new Function(...keys, `return (${async ? 'async ' : ''}(${keys.join(',')}) => { ${fn} })(${keys.join(',')})`)(...keys.map(k => (<any>args)[k]));
}
/**