查看此switch case语句。案例“响应”和“错误”有很多共同点,但也有一条线可以区分它们。
switch (data.type) {
case 'response':
const transactionId = data.trans_id;
const callbacks = commandCallbacks[transactionId];
if (!callbacks) {
if (logger) logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
break;
}
clearTimeout(callbacks.timedOutTimer);
callbacks.resolver(data);
break;
case 'error':
const transactionId = data.trans_id;
const callbacks = commandCallbacks[transactionId];
if (!callbacks) {
if (logger) logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
break;
}
clearTimeout(callbacks.timedOutTimer);
callbacks.rejecter(data);
break;
case 'information':
case 'progress':
default:
break;
}
如何简化此代码?
答案 0 :(得分:1)
switch (data.type) {
case 'error':
case 'response':
const transactionId = data.trans_id;
const callbacks = commandCallbacks[transactionId];
if (!callbacks) {
if (logger) logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
break;
}
clearTimeout(callbacks.timedOutTimer);
data.type === 'response' ? callbacks.resolver(data) : callbacks.rejecter(data);
break;
case 'information':
case 'progress':
default:
break;
}
此外,如果您还有其他要签入的选项,则可以将其更改为if
。
答案 1 :(得分:1)
使用方括号表示法和条件运算符访问callbacks
上的相应函数属性,然后调用该函数:
switch (data.type) {
case 'response':
case 'error':
const transactionId = data.trans_id;
const callbacks = commandCallbacks[transactionId];
if (!callbacks) {
if (logger) logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
break;
}
clearTimeout(callbacks.timedOutTimer);
const fn = callbacks[data.type === 'response' ? 'resolver' : 'rejecter'];
fn(data);
break;
// ...
请注意,如果函数依赖于this
是callbacks
,请改用.call
:
fn.call(callbacks, data);
这实际上与OR和XOR没有任何关系-毕竟,不同的case
是互斥的。
答案 2 :(得分:1)
通过移动您的response
语句,使error
情况“陷入” case
情况:
switch (data.type) {
case 'response':
case 'error':
const transactionId = data.trans_id;
const callbacks = commandCallbacks[transactionId];
if (!callbacks) {
if (logger) logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
break;
}
clearTimeout(callbacks.timedOutTimer);
if(data.type === 'response') {
callbacks.resolver(data);
} else if(data.type === 'error' {
callbacks.rejecter(data);
}
break;
case 'information':
case 'progress':
}
答案 3 :(得分:1)
您可以使用一个函数来获取回调,并稍后使用具有正确目标值的对象对其进行调用。
function getCallbacks({ trans_id }) {
const callbacks = commandCallbacks[trans_id];
if (!callbacks) {
if (logger) logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
return;
}
return callbacks;
}
switch (data.type) {
case 'response':
case 'error':
let cb = getCallbacks(data);
if (!cb) break;
clearTimeout(cb.timedOutTimer);
cb[{ response: 'resolver', error: 'rejecter' }[data.type]](data);
}
答案 4 :(得分:1)
我将完全跳过switch语句,并这样写:
if (["reponse", "error"].includes(data.type)) {
const transactionId = data.trans_id;
const callbacks = commandCallbacks[transactionId];
if (callbacks) {
const callback = {
response: data => callbacks.resolver(data),
error: data => callbacks.rejecter(data)
}[data.type];
clearTimeout(callbacks.timedOutTimer);
callback(data);
} else if (logger) {
logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
}
}
通常,我更喜欢使用对象进行切换。
答案 5 :(得分:0)
这是我的最佳尝试:
switch (data.type) {
case 'response':
case 'error':
const transactionId = data.trans_id;
const callbacks = commandCallbacks[transactionId];
if (!callbacks) {
if (logger) logger(`Dropping received message that was not requested, transactionId: ${transactionId}`);
break;
}
clearTimeout(callbacks.timedOutTimer);
switch (data.type) {
case 'response':
callbacks.resolver(data);
case 'error':
callbacks.rejecter(data);
}
break;
case 'information':
case 'progress':
default:
break;
}