Coffeescript 咖啡脚本中的连锁承诺

Coffeescript 咖啡脚本中的连锁承诺,coffeescript,Coffeescript,有没有办法在Coffeescript中将承诺链接在一起。例如,考虑下面的JavaScript代码, return $.getJSON('/api/post.json') .then(function(response) { // do something }) .then(function(response) { // do something }) .then(null, function(err) { // do something });

有没有办法在Coffeescript中将
承诺
链接在一起。例如,考虑下面的JavaScript代码,

return $.getJSON('/api/post.json')
  .then(function(response) {
    // do something
  })
  .then(function(response) {
    // do something
  })
  .then(null, function(err) {
    // do something
  });
每个
then的
都是可选的,函数需要返回最终的
then
。 目前我正在用咖啡脚本写这篇文章

promise = $.getJSON('/api/post.json')
promise = promise.then (response) ->
  // do something

promise = promise.then (response) ->
  // do something

promise = promise.then null, (err) ->
  // do something

return promise

有更好的方法吗?谢谢。

这可能是您最好的选择:

$.getJSON('/api/post.json')
    .then( (response) ->
      # do something
    ).then( (response) ->
      # do something
    ).then null, (err) ->
      # do something

注意
then()
参数周围的括号。没有什么惊天动地的事情,但希望这会有所帮助。

以西结指出了正确的方法,但它不需要在函数周围加括号。只要做:

$.getJSON '/api/post.json' # As of CoffeeScript 1.7, you don't need the parentheses here either.
.then (response) ->
  # do something
  response # if you would not return anything, promise would be fulfilled with undefined
.then (response) ->
  # do something
  undefined # necessary to prevent empty function body
.then null, (err) ->
  # handle error
我觉得它干净得出奇。 一件相对混乱的事情是,当您需要同时添加onRejected和onCompleted处理程序时

注意:上次我检查时,这在CoffeeScript Redux中不起作用,但这是几个月前的事了


注2:每个函数体中至少需要一行实际代码(即,不只是注释)才能工作。通常,你会的,所以这不是一个大问题。

这是我个人最喜欢的写承诺的方式,有一点额外的缩进

doSomething = () -> new RSVP.Promise (resolve, reject) ->
  if 1 is 1
    resolve 'Success'
  else
    reject 'Error'

doSomething()
.then (res) ->
      console.log 'Step 1 Success Handler'

    , (err) ->
      console.log 'Step 1 Error Handler'

.then (res) ->
      console.log 'Step 2 Success Handler'

.then (res) ->
      console.log 'Step 3 Success Handler'

    , (err) ->
      console.log 'Step 3 Error Handler'
其汇编目的是:

var doSomething;

doSomething = function() {
  return new RSVP.Promise(function(resolve, reject) {
    if (1 === 1) {
      return resolve('Success');
    } else {
      return reject('Error');
    }
  });
};

doSomething().then(function(res) {
  return console.log('Step 1 Success Handler');
}, function(err) {
  return console.log('Step 1 Error Handler');
}).then(function(res) {
  return console.log('Step 2 Success Handler');
}).then(function(res) {
  return console.log('Step 3 Success Handler');
}, function(err) {
  return console.log('Step 3 Error Handler');
});
在某些情况下,这也非常有效:

step1Success = (res) -> console.log 'Step 1 Success Handler'
step1Error   = (err) -> console.log 'Step 1 Error Handler'

step2Success = (res) -> console.log 'Step 2 Success Handler'

step3Success = (res) -> console.log 'Step 3 Success Handler'
step3Error   = (err) -> console.log 'Step 3 Error Handler'

doSomething()
  .then(step1Success, step1Error)
  .then(step2Success)
  .then(step3Success, step3Error)

在coffee script v1.6.3上进行了测试,您还可以查看IcedCoffeeScript。它的工作原理有点不同,但它工作得很好。谢谢。正如@Meryn Stol所说,松开括号会让事情变得更好。松开括号?我猜你的意思是失去他们。一想到要在世界上丢括号,真的很可怕。投票被否决了,因为它没有在CoffeeScript 1.4.0或1.6.3(最新版本)中编译。我的回答中的括号是必需的。@EzekielVictor:谢谢你的警惕。我只是研究了一下,问题似乎是链条的第二部分缺少一个功能体。缩进的评论似乎还不够。我在末尾添加了一个显式的“undefined”,这样它就可以编译了。我从来没有遇到过这个问题,因为我从来没有在这些地方有过非空的函数体。啊,谢谢你的回答。我删除了DV。。。我在那里学到了一些有用的东西。getJSON('/api/post.json'.then(函数(响应){return response;})。then(函数(响应){return void 0;})。then(null,函数(err){});注2很重要。在过去的20分钟里,我一直在想到底出了什么问题。Promissions中单独的注释无法使用coffeescript工作或编译成功。这很好地工作,唯一让我有点担心的是确保代码中没有空格。对于那些刚接触coffeescript的人来说,这可能很常见,但在我意识到选项卡集中只有一个空格之前,我无法编译上述代码。