1

以下に、jQuerydeferredを使用するサンプルコードがあります。私が理解できないように見えるのは、brushTeeth関数が拒否されたpromiseを返すのに、なぜ別の遅延であるcollectionResultsが常に解決されるのかということです。

一部のjQueryの遅延読み取りでは、$。whenで渡された関数がpromiseでない場合、それらはすぐに解決されますが、brushTeethは実際にはpromiseを返します。

私がここで間違っていることの手がかり?

ShowerModule = ( function($) {
                    function init(){
                        var result = $.Deferred();
                        var collectionResults = $.when(brushTeeth);
                        collectionResults.done(function(){
                            console.log("done");
                        })

                        collectionResults.fail(function(){
                            console.log("reject");
                        })

                    }

                    function brushTeeth() {
                        var result = $.Deferred();
                        result.reject('["bah"]');
                        return result.promise();

                    }

                    return {
                        init : init
                    }

                }(jQuery));
            ShowerModule.init();
4

2 に答える 2

1

理解した

var collectionResults = $.when(brushTeeth);

上記の行は

var collectionResults = $.when(brushTeeth());
于 2013-03-08T06:29:09.843 に答える
0

同じ名前の2つの遅延オブジェクトを作成しましたresult

だから私は$.whenがその上のものを約束として取っていると思います..これは拒否されません..これを試してください

 function init(){
                   // var result = $.Deferred(); remove this
                    var collectionResults = $.when(brushTeeth()); //accept function and not the function's reference
                    collectionResults.done(function(){
                        console.log("done");
                    })

                    collectionResults.fail(function(){
                        console.log("reject");
                    })

                }
于 2013-03-08T06:12:08.707 に答える