如果需要来自两个 URL 的数据,如何堆叠 dojo 请求
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
你的两个请求都会return给你一个承诺。 dojo/promise/all
模块完全满足您的需求:等待两个 Promise 解决,然后您可以对响应做任何您需要的事情。有关 all
模块的更多信息,请参阅 link.
在你的例子中,代码应该是这样的:
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
});
}
如果我需要来自两个来源的数据才能继续,我该如何堆叠 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
你的两个请求都会return给你一个承诺。 dojo/promise/all
模块完全满足您的需求:等待两个 Promise 解决,然后您可以对响应做任何您需要的事情。有关 all
模块的更多信息,请参阅 link.
在你的例子中,代码应该是这样的:
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
});
}