[英]how to stack dojo request if data from two URLs needed
如果我需要來自兩個來源的數據以繼續,該如何堆疊dojo請求函數?
以下內容不起作用,並且可能僅在file1.json之后才開始加載file2.json,盡管此時沒有依賴性:
require(["dojo/request"], function(request){
request("file1.json", {handleAs: "json"}).then(function(datajson1){
request("file2.json", {handleAs: "json"}).then(function(datajson2){
use datajson1 and datajson2 here
您的兩個請求都將退還給您。 dojo/promise/all
模塊完全滿足您的需求:等待兩個Promises解決,然后您就可以對響應做任何您需要的事情。 有關all
模塊的更多信息,請參見鏈接 。
在這種情況下,代碼應類似於:
require(["dojo/promise/all", "dojo/request"], function(all, request) {
var promiseA = request("file1.json", {handleAs: "json"}),
promiseB = request("file2.json", {handleAs: "json"});
all([promiseA, promiseB).then(function(results) {
// Results is the array with the promises results.
// results[0] will be the return from promiseA
// results[1] will be the return from promiseB
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.