鏈接用是有沒有辦法鏈Promises
在一起的CoffeeScript。例如,考慮下面的JavaScript代碼,在CoffeeScript中
then's
的
return $.getJSON('/api/post.json')
.then(function(response) {
// do something
})
.then(function(response) {
// do something
})
.then(null, function(err) {
// do something
});
每個是可選的,並且需要最終then
由函數返回。 目前,我在CoffeeScript中爲寫這個,
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
有沒有更好的方式來做到這一點?謝謝。
你也可以看看IcedCoffeeScript。它的工作方式有點不同,但工作得很好。 – 2013-07-07 17:18:17