簡體   English   中英

jQuery.when()無法按預期工作

[英]jQuery.when() not working as expected

我有一系列異步操作,它們的響應必須同步才能運行最終操作。 我正在使用Deferred對象以及when()done()方法來實現此目的,但是由於某些原因,在調用響應的第一個resolve()時,done()中的最終操作總是執行的。

這是我的代碼,請縮短一點以使其更清楚:

       // Central function that creates a Deferred object and returns a
       // Promise from it. when the passed request is done, 
       // the Deferred is resolved
       var getData = function(url, successFunction) {
            var deferred = $.Deferred();
            $.ajax({
                url: url,
                method: 'get',
                dataType: 'json'
            }).done(function(p) {
                successFunction(p);
                deferred.resolve(p);
            }).fail(function(p){
                deferred.reject(p);
            });
            return deferred.promise();
        };

        // Success actions to be called for each asynchronous request

        var populateDestinations = function(data) {
            // synchronous actions on DOM
        };

        var populateTaxes = function(data) {
            // synchronous actions on DOM
        };

        var populatePayment = function(data) {
            // synchronous actions on DOM
        };


        // Actions that return a Promise to control the resolution of 
        // all the deferred objects

        var getCustomerDestinations = function(customerId) {
            var url = $modal.data('url_destinations') + customerId;
            return getData(url, populateDestinations);
        };

        var getCustomerTaxes = function(customerId) {
            var url = $modal.data('url_taxes') + customerId;
            return getData(url, populateTaxes);
        };

        var getCustomerPayment = function(customerId) {
            var url = $modal.data('url_payment') + customerId;
            return getData(url, populatePayment);
        };

        var populateFields = function() {
            // final actions
        };


        $.when(getCustomerDestinations(customer_id),
                getCustomerTaxes(customer_id),
                getCustomerPayment(customer_id))
        .done(function(){

             populateFields();

        });

只要解決了“應許”功能之一,而不是解決了所有功能時,就會調用populateFields()

知道我在做什么錯嗎? 也許我還沒有掌握Deferred對象的概念。

您確實不需要使用任何延遲的對象來跟蹤ajax調用,而是可以只使用$.when()$.ajax返回的promise對象。

JQUERY代碼:

var getData = function(url, successFunction) {
        return $.ajax({
            url: url,
            method: 'get',
            dataType: 'json'
        }).then(function(p) {
            successFunction(p);
        },function(p){
            //process error using error callback, 
            //just like success callbacks
        });
    };

我為了處理單個ajax調用,可以使用.then()而不是.done().fail() ,因為它們不會像.fail()那樣返回任何promise對象.then()以在.when()進行跟蹤。

您的其余代碼將按原樣工作。

jQuery論壇怎么說:

jQuery 1.8開始deferred.then()方法返回一個新的promise,可以通過函數過濾deferred的狀態和值,從而替換了現在不推薦使用的deferred.pipe()方法。 doneFilterfailFilter函數過濾原始延遲的已解析/已拒絕狀態和值。 progressFilter函數過濾對原始延遲的notify或notifyWith方法的所有調用。 這些過濾器函數可以返回要傳遞給.fail().done().fail()回調的新值,也可以返回另一個可觀察對象(Deferred,Promise等),該對象將傳遞其已解析/拒絕的狀態和值到諾言的回調。

參考鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM