eltro/lib/callback.mjs

33 lines
707 B
JavaScript
Raw Permalink Normal View History

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)
}
})
}