Skip to content

Instantly share code, notes, and snippets.

@izelnakri
Last active February 26, 2025 01:56
Show Gist options
  • Save izelnakri/a005a0b25a4a1bbf309ebefb1e3e143f to your computer and use it in GitHub Desktop.
Save izelnakri/a005a0b25a4a1bbf309ebefb1e3e143f to your computer and use it in GitHub Desktop.
Pcall.js Lua pcall in JavaScript
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