eltro/lib/callback.mjs
Jonatan Nilsson 9d2b71339c
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
Added helpers into cb:
* wrap() that auto captures thrown exceptions and cb's them
* finish() that auto captures thrown exception and cb's them, otherwise auto-finishes the cb for you
2022-07-04 10:23:41 +00:00

33 lines
No EOL
707 B
JavaScript

export function runWithCallbackSafe(test) {
return new Promise(function(res, rej) {
try {
let cb = function(err) {
if (err) {
return rej(err)
}
res()
}
let safeWrap = function(finish) {
// return a safe wrap support
return function(fun) {
return function(a, b, c) {
try {
fun(a, b, c)
if (finish) {
res()
}
}
catch (err) {
return rej(err)
}
}
}
}
cb.wrap = safeWrap(false)
cb.finish = safeWrap(true)
test.func(cb)
} catch (err) {
rej(err)
}
})
}