jQuery のDeferred
システムの用途の 1 つは、$.when()
関数を使用することです。可変数のPromise's and will do something when they all
リゾルブ(or when the first
リジェクトが必要です。)
JSON
たとえば、2 つの Ajaxクエリを操作したい場合は、次のようにします。
var funkyPromise = $.getJSON('http://funky.json.service.com');
var awesomePromise = $.getJSON('http://awesome.json.service.com');
$.when(funkyPromise, awesomePromise).then(function() {
/* do something amazing with the two objects */
}, function() {
/* at least one of the services failed this time */
});
jQuery のシステムでできるもう 1 つのことは、最終的にメソッドを使用する前に、メソッドを使用して、あるデータから別Deferred
のデータに「連鎖」することにより、データ パイプラインを作成することです。Deferred
pipe
$.getJSON('http://funky.json.service.com')
.pipe(funkytoFunkier);
}).done(function(funkyData) {
/* we now have a funkier version of what the web service gave us */
});
すべてが見事に非同期で分離されています。
$.when()
しかし、2 つの非同期s で使用したいのにPromise
、非同期で渡されるため、まだ 1 つも持っていない場合はどうなりpipe
ますか?
var funkyPromise = $.getJSON('http://funky.json.service.com');
var awesomePromise = $.getJSON('http://awesome.json.service.com');
// run "funky" through an asynchronous "pipe" to get "funkier"
//
// ... but ... how ??
$.when(funkyier, awesome).then(function() {
/* do something amazing with the two objects */
}, function() {
/* at least one of the services failed this time */
});
では、中間セクションには何が入りますか?
- それは盲目的に明白で、私には見えないだけですか?
- それは可能ですが、非常に微妙でトリッキーなものですか?
pipe()
またはなどの新しい「反転」同等物は$.when()
それをより簡単にしますか?