From b07ce3ee7c9a69893e6118f6adfc40efa99fada4 Mon Sep 17 00:00:00 2001 From: anotherso1a <1181581742@qq.com> Date: Wed, 6 Mar 2024 14:48:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4promiify=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/api-proxy/src/common/js/promisify.js | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/packages/api-proxy/src/common/js/promisify.js b/packages/api-proxy/src/common/js/promisify.js index 5bb3159697..79da09887c 100644 --- a/packages/api-proxy/src/common/js/promisify.js +++ b/packages/api-proxy/src/common/js/promisify.js @@ -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) } } })