2013-07-07 6 views
25

PromisesをCoffeescriptで結ぶ方法はありますか?例えば、次のJavaScriptコードを考慮し、then'sCoffeescriptで約束を結ぶ

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 

はこれを行うには良い方法はありますか?ありがとう。

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

then()引数を囲む括弧:

+0

また、IcedCoffeeScriptを見ることもできます。少し違って動作しますが、かなりうまく動作します。 –

答えて

40

Ezekielは正しい方法を示していますが、関数の周りにかっこは必要ありません。ちょうど:

$.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 

私はそれが驚くほどきれいだと思います。 比較的厄介なことの1つは、onRejectedハンドラとonFulfilledハンドラを同時に追加する必要がある場合です。

注:前回チェックしたとき、これはCoffeeScript Reduxでは機能しませんでしたが、これは数ヶ月前です。

注2:これを機能させるには、各機能体に少なくとも1行の実際のコード(コメントだけでなく)が必要です。一般的に、あなたはそうです、それは大きな問題ではありません。

+0

ありがとうございます。これはCoffeescriptコンパイラで動作します。 –

+0

CoffeeScript 1.4.0または1.6.3(最新版)ではコンパイルされないため、Downvoted。私の答えに示されているかっこが必要です。 –

+4

@エゼキエルビジター:警戒してくれてありがとう。私はちょうどそれを調べました、そして問題は、チェーンの第二の部分が機能体を欠いているように思われます。インデントされたコメントは一見不十分だった。最後に明示的な "undefined"を追加してコンパイルします。私はこれらの場所に空ではない機能の本体を持っていないので、この問題に遭遇したことはありません。 –

4

これはおそらく、あなたがやるのが最善です。地球は壊れていませんが、うまくいけばこれが助けになります。

+0

ありがとうございます。 @Meryn Stolが示唆しているように、かっこを失うと、それはより良いものになります。 –

+3

カッコが紛れていませんか?私はそれらを失うことを意味すると思います。世界にかっこを失うという考えは、本当に恐ろしいことです。 –

12

これはにコンパイル少し余分にインデント

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) 

テスト済みのコーヒースクリプトv1.6.3

+0

これはうまく動作し、ちょっと私を抱きしめたのは、コードにスペースがないことを保証することだけです。これはcoffeescriptを初めて使う人にはよくあることですが、タブのセット内に1つのスペースがあることがわかるまで、上記のようなコードをコンパイルすることはできませんでした。 –