Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PromiseとDeferredの違い #11

Closed
azu opened this issue May 4, 2014 · 5 comments
Closed

PromiseとDeferredの違い #11

azu opened this issue May 4, 2014 · 5 comments
Milestone

Comments

@azu
Copy link
Owner

azu commented May 4, 2014

PromiseとDeferredの違いについて

Deferredは

Twisted -> MochiKit/Dojo -> Promises/A -> jQuery.Deferred

時系列適当…

Deferred ∋ Promise という関係は明確だけど、細かい仕様はPromise/A+とかみたいなのがないきがするから、イマイチDeferredと言われた時に何をしめすものなのか分からない。(たいていはjQeuryになるんだろうけど)

正直そこまで詳しくないし、あまり興味持ってないから寄稿して欲しい感じ…

@azu azu added the new-section label May 4, 2014
@azu
Copy link
Owner Author

azu commented May 4, 2014

Promise vs jQuery という趣旨にするならば、以下に書かれてるonRejectedの引数が異なるために起きる問題について触れておいたほうが実用的な話になると思う。

むしろ、Promiseと合わせて使うことができるよ、普通にやるとエラー情報が取れないケースがあるかもよ、その解決方法について

という感じのがあれば良い気がする。

@azu
Copy link
Owner Author

azu commented May 4, 2014

Promise.resolve と相性が良くない話

var jqDeferred = Promise.resolve($.ajax('http://example.com/whatever.json'));

jqDeferred.then(function(response, statusText, xhrObj) {
  console.info("Success");
}, function(xhrObj, textStatus, err) {
   console.error(arguments);
});

http://jsbin.com/ruzunena/1/edit

jQueryとPromise/A+辺りの違いについて

@azu
Copy link
Owner Author

azu commented May 4, 2014

細かい違い(Deferredが実装次第で揺れてしまうため)が沢山あったり、
多分jQuery Deferredもちゃんと理解してる人はそこまで多くないので、書く内容のバランスが難しい気がする。

両方に詳しい人が書いてくれるのがベストかなーと思う所。

全部について触れるのは結構きつそうなので、ここだという所を一点ぐらいでいいかもしれないなー

@azu
Copy link
Owner Author

azu commented May 5, 2014

シンプルなDeferred top on Promiseの例

"use strict";
function Deferred() {
    this.promise = new Promise(function (resolve, reject) {
        this._resolve = resolve;
        this._reject = reject;
    }.bind(this));
}
Deferred.prototype.resolve = function (value) {
    this._resolve.call(this.promise, value);
};
Deferred.prototype.reject = function (reason) {
    this._reject.call(this.promise, reason);
};

var deferred = new Deferred();
var promise = deferred.promise;
promise.then(function (value) {
    console.log(value);
}).catch(function(error){
    console.error(error);
});

deferred.resolve("Fulfilled!!!!");
// promiseは既にFulfilledになってるので、これは呼ばれない(catchは呼ばれない)
deferred.reject("doesn't called");

http://jsbin.com/qumabane/3/edit

名前の通り、promiseオブジェクトの状態を後からdeferredが解決したりできる。
(2度解決はpromise上に作ってるので普通にできない

@azu azu mentioned this issue May 5, 2014
4 tasks
@azu
Copy link
Owner Author

azu commented May 5, 2014

#15 でかなり簡略化したPromiseとDeferredの関係について書いた。

http://azu.github.io/promises-book/#_deferred_promise

一応閉じますが、かなりぼかした感じなのでより詳細なのも継続して受け付けていますー


@azu azu closed this as completed May 5, 2014
@azu azu added this to the 1.0.0 milestone May 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant