0

假設我們只有一個承諾對象,如下所示。

var myPromise = $.get(url1); 
myPromise.done(function(data){ 
    console.log(data); 
}); 

我們能夠從承諾對象訪問數據。現在假設,我們已經通過$。當

var multiplePromises = $.when($.get(url1),$.get(url2),$.get(url3)); 
multiplePromises.done(function(){ 

}); 

解決多個承諾對象的上述要求已得到滿足,也就是說,只有當所有的GET請求完成後,做了一部分應該得到執行。但是,如何從$ .when.done()方法中分別得到來自每個get的數據響應?

+1

你閱讀[文件](https://api.jquery.com/jQuery.when/)? *「傳遞給doneCallbacks的參數爲每個Deferreds提供解析值,並將Deferreds傳遞給jQuery.when()的順序進行匹配。」​​ –

回答

1

您將它們作爲參數。

function get(what) { 
 
    return $.when(what) 
 
} 
 

 
$.when(get(1), get(2), get(3)).done(function(first, second, third) { 
 
    console.log(first, second, third) 
 
})
<script src="https://unpkg.com/[email protected]/dist/jquery.js"></script>

相關問題