簡體   English   中英

在ajax調用promise中的.done()函數之前使用回調函數

[英]use a callback function before a .done() function in ajax call promises

這是我要實現的示意圖:

function loadLotsofSomething() {
    for (var i=0;i<1000;i++) {
        results.push(loadOneSomething(i))
    }

    $.when.apply(this,results).done(function() {
        for (var i = 0; i < arguments.length; i++) {
            //get doSomehting(data) instead of data here
        }
    }   
}

function loadOneSomething(i) {
    return $.ajax({
        async : true,
        url: url,
        success: function(data){
            return doSomething(data);
        }
    });
}

function doSomething (x) {
    return x;
}

我希望success函數在done函數之前執行,因為在循環所有調用之前修改從ajax調用獲取的數據更簡單。

就我而言,無論我將什么放入success函數中,我總是在done函數中獲取原始數據。

任何幫助歡迎!

您想使用鏈接 (就像promise一樣)。 根本不要使用success 而是使用then並返回其結果:

function loadOneSomething(i) {
    return $.ajax({
        async : true,
        url: url
    }).then(function(data){
        return doSomething(data);
    });
}

這樣, loadOneSomething返回的loadOneSomethingloadOneSomething起的then ,而不是$.ajax那個,並且它的解析值是您then返回的。

更多: $.ajax的jqXHR條目deferred.then

暫無
暫無

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

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