const execute = (cb) => {
setTimeout(() => {
try {
Promise.resolve(cb()).catch(() => void 0);
} catch {}
}, 0);
}
const factorial = (x) => {
if (typeof x !== 'bigint') x = BigInt(x);
return new Promise((resolve) => {
let result = x;
const calc = () => {
execute(() => {
x--;
if (x <= 0n) return void resolve(result);
result *= x;
calc();
});
};
calc();
});
}
factorial('hyh').then(console.log);