2022-07-04 10:23:41 +00:00
|
|
|
export function runWithCallbackSafe(test) {
|
2023-09-28 09:33:43 +00:00
|
|
|
let finished = false
|
2022-07-04 10:23:41 +00:00
|
|
|
return new Promise(function(res, rej) {
|
|
|
|
try {
|
|
|
|
let cb = function(err) {
|
|
|
|
if (err) {
|
|
|
|
return rej(err)
|
|
|
|
}
|
|
|
|
res()
|
|
|
|
}
|
2023-09-28 09:33:43 +00:00
|
|
|
let safeWrap = function(fn, ...args) {
|
|
|
|
try {
|
|
|
|
return fn(...args)
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return rej(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let safeWrapInFunction = function(finish, fn) {
|
|
|
|
return function(...args) {
|
|
|
|
safeWrap(fn, ...args)
|
|
|
|
if (finish && !finished) { res() }
|
2022-07-04 10:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-28 09:33:43 +00:00
|
|
|
cb.wrap = safeWrapInFunction.bind(this, false)
|
|
|
|
cb.finish = safeWrapInFunction.bind(this, true)
|
|
|
|
cb.safeWrap = safeWrap
|
2022-07-04 10:23:41 +00:00
|
|
|
test.func(cb)
|
|
|
|
} catch (err) {
|
|
|
|
rej(err)
|
|
|
|
}
|
|
|
|
})
|
2023-09-28 09:33:43 +00:00
|
|
|
.then(
|
|
|
|
function() { finished = true },
|
|
|
|
function(err) { finished = true; return Promise.reject(err) }
|
|
|
|
)
|
2022-07-04 10:23:41 +00:00
|
|
|
}
|