Last active
February 26, 2025 01:56
-
-
Save izelnakri/a005a0b25a4a1bbf309ebefb1e3e143f to your computer and use it in GitHub Desktop.
Pcall.js Lua pcall in JavaScript
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
async function exampleFunc(greeter?: string, another): Promise<string> { | |
if (!greeter) { | |
throw new Error('Somethinggg', another); | |
} | |
return `Hello, ${greeter} ${another}`; | |
} | |
let pcall = (func: Promise<Any> | Function, ...otherArguments: any) => { | |
if (func instanceof Promise) { | |
return new Promise(async (resolve) => { | |
let error | |
try { | |
return resolve([true, await func]) | |
} catch (err) { | |
error = err | |
} | |
return resolve([false, error]) | |
}) | |
} | |
try { | |
const result = func(...otherArguments); | |
return [true, result]; // Success, return result | |
} catch (error) { | |
return [false, error]; // Error occurred, return the error | |
} | |
} | |
async function main() { | |
let result = await pcall(Promise.all([ | |
exampleFunc('Izel'), | |
exampleFunc() | |
])) | |
console.log("result is:", result) | |
return result; | |
} | |
await main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment