Coffeescript承诺使用多行语法
我在Coffeescript中有以下代码:Coffeescript承诺使用多行语法,coffeescript,Coffeescript,我在Coffeescript中有以下代码: @update().success((company)=> ).error((company)=> ).finally((company)=> ) 我想知道是否可以将其更改为删除一些括号。大概是这样的: @update() .success (company)=> .error (company)=> .finally (company)=> 但我总是得到一
@update().success((company)=>
).error((company)=>
).finally((company)=>
)
我想知道是否可以将其更改为删除一些括号。大概是这样的:
@update()
.success (company)=>
.error (company)=>
.finally (company)=>
但我总是得到一个语法错误:[stdin]:18:9:意外。
。知道我做错了什么吗
谢谢。如果你愿意忍受分号:
@update()
.success (company)=>;
.error (company)=>;
.finally (company)=>;
如果你愿意忍受分号:
@update()
.success (company)=>;
.error (company)=>;
.finally (company)=>;
只要填充函数体,该语法就可以正常工作:
@update()
.success (company) => true
.error (company) => true
.finally (company) => true
否则,您必须清楚地描述回调函数:
@update()
.success ((company) =>)
.error ((company) =>)
.finally ((company) =>)
但话说回来,您首先不会编写空回调。只要您填写函数体,该语法就可以正常工作:
@update()
.success (company) => true
.error (company) => true
.finally (company) => true
否则,您必须清楚地描述回调函数:
@update()
.success ((company) =>)
.error ((company) =>)
.finally ((company) =>)
但话说回来,你一开始不会写一个空回调。你就快到了,你只是忘了添加
return
语句:
@update()
.success (company)=>
return
.error (company)=>
return
.finally (company)=>
return
您就快到了,只是忘记添加
return
语句:
@update()
.success (company)=>
return
.error (company)=>
return
.finally (company)=>
return
仅仅因为你可以使用匿名函数并不意味着你必须这么做。分解函数并给它们命名通常会使代码更清晰、更易于阅读,并减少空白 例如:
frobnicate_the_things = => # some pile of logic goes here
complain_about_the_problems = => # and another pile here
clean_up_the_mess = => # and yet another here
@update()
.success frobnicate_the_things
.error complain_about_the_problems
.finally clean_up_the_mess
当然,我不知道你的回调实际上在做什么,所以我不得不编一些愚蠢的名字,但这不是重点。关键是你有很好的自文档化代码,而不需要在语法和空格上乱动。仅仅因为你可以使用匿名函数并不意味着你必须这样做。分解函数并给它们命名通常会使代码更清晰、更易于阅读,并减少空白 例如:
frobnicate_the_things = => # some pile of logic goes here
complain_about_the_problems = => # and another pile here
clean_up_the_mess = => # and yet another here
@update()
.success frobnicate_the_things
.error complain_about_the_problems
.finally clean_up_the_mess
当然,我不知道你的回调实际上在做什么,所以我不得不编一些愚蠢的名字,但这不是重点。关键是,您有很好的自文档代码,而不需要大量修改语法和空格