如何在不重复“DoTheNextThing();”的情况下完成此操作?
if (dosomething)
DoSomething(function () {
DoTheNextThing();
})
else
DoTheNextThing();
我没有尝试过其他任何东西,因为我想不出还有什么可以尝试。
这并不漂亮,但您可以使用一个只调用其参数的函数,并将其有条件地分配给一个变量。
const f = dosomething ? DoSomething : cb => cb();
f(DoTheNextThing);
而不是
doSomething
,选择一个不执行任何操作的函数,然后调用回调:
function DoNothing(callback) { callback(); }
(dosomething ? DoSomething : DoNothing)(DoTheNextThing);