33 lines
707 B
JavaScript
33 lines
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)
|
||
|
}
|
||
|
})
|
||
|
}
|