在三元表达式中使用return作为多个语句之一 [英] Using return as one of multiple statements in ternary expression
本文介绍了在三元表达式中使用return作为多个语句之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
err ? (reject(err), return)
: resolve(db)
其中哪个返回值:
SyntaxError:意外的令牌返回
SyntaxError: Unexpected token return
这项工作:
err ? (reject(err), console.log('test'))
: resolve(db)
为什么在这种情况下不能使用退货?还有其他替代方法,可以在对多条语句使用三元运算符时停止函数执行?
Why is that return can't be used in this situation? Is there other alternative to stop function execution while using ternary operator for multiple statements?
推荐答案
这是三元表达式,整个表达式必须求值,因此只能包含表达式。
It's a ternary expression, the expression as a whole must evaluate to a value, and thus contain only expressions.
您不能说 a = 1 +
You can't say a = 1 + return;
either.
还有其他方法可以在使用
三元运算符进行运算时停止函数执行
Is there other alternative to stop function execution while using ternary operator for multiple statements?
if
语句...
if (err) { reject(err); return }
resolve(db);
这篇关于在三元表达式中使用return作为多个语句之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文