Skip to content

Commit

Permalink
refactor: 调整promiify代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherso1a committed Mar 6, 2024
1 parent a2e3c80 commit b07ce3e
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions packages/api-proxy/src/common/js/promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,20 @@ function promisify (listObj, whiteList, customBlackList) {
}

result[key] = function (...args) {
const obj = args[0]
if (promisifyFilter(key) && !(obj?.success || obj?.fail || obj?.complete)) {
if (!args[0]) {
args.unshift({ success: noop, fail: noop })
}
// 不需要转换 or 用户已定义回调,则不处理
if (!promisifyFilter(key) || ['success', 'fail', 'complete'].some(k => args[0] && args[0][k])) {
return listObj[key].apply(envObj, args)
} else { // 其他情况进行转换
const obj = args[0] || { success: noop, fail: noop }
if (!args[0]) args.unshift(obj)
let returned
const promise = new Promise((resolve, reject) => {
obj.success = function (res) {
resolve(res)
}
obj.fail = function (e) {
reject(e)
}
obj.success = resolve
obj.fail = reject
returned = listObj[key].apply(envObj, args)
})
promise.__returned = returned
return promise
} else {
return listObj[key].apply(envObj, args)
}
}
})
Expand Down

0 comments on commit b07ce3e

Please sign in to comment.