LiveScript - 如果有多个回调,如何使用后退调用符?

gsk*_*lee 2 javascript livescript

例如:

$.get('/path/to/api').then(
  function(data) {
    alert( "$.get succeeded" );
  }, function(error) {
    alert( "$.get failed!" );
  }
);
Run Code Online (Sandbox Code Playgroud)

是否可以在两个回调中应用回调运算符?

小智 7

你只能使用一个函数和backcall,但你可以做的是使用一个回调,并正常提供另一个函数(注意使用_占位符来表示backcall函数应该在参数中的位置) - 例如.

data <- $.get '/path/to/api' .then _, -> alert "$.get failed!"
alert "$.get succeeded"
Run Code Online (Sandbox Code Playgroud)

编译为:

$.get('/path/to/api').then(function(data){
  return alert("$.get succeeded");
}, function(){
  return alert("$.get failed!");
});
Run Code Online (Sandbox Code Playgroud)