我有几个查询任务,其中一些还可能包含查询任务。如何使用dojo延迟确保查询完成然后运行其他代码?
这是JavaScript伪代码:
// About queryTask: https://developers.arcgis.com/javascript/3/jsapi/querytask-amd.html
var data=[];
settings for query1...;
queryTask1.execute(query1);
queryTask1.on("complete", function (evt1)
{
var fset1 = evt1.featureSet;
var resultFeatures1 = fset1.features;
...
settings for query2...;
queryTask2.execute(query2);
queryTask2.on("complete", function (evt2)
{
var fset2 = evt2.featureSet;
var resultFeatures2 = fset2.features;
...
});
});
settings for query3...;
queryTask3.execute(query3);
queryTask3.on("complete", function (evt3)
{
var fset3 = evt3.featureSet;
var resultFeatures3 = fset3.features;
...
});
ResultToKendoGrid(data); //data is updated in the query task 1 2 and 3
答案 0 :(得分:0)
您可以将查询任务包装在dojo/Deferred
个对象中,并在任务完成/拒绝时解析主题。
如果您需要运行一些代码,那么当您的所有查询任务都完成后,您的所有dojo延迟对象都会被解析,您可以使用dojo/promise/all
仅在完成所有延迟时才执行。
文档:
https://dojotoolkit.org/reference-guide/1.10/dojo/Deferred.html
https://dojotoolkit.org/reference-guide/1.10/dojo/promise/all.html
require(['dojo/Deferred', 'dojo/promise/all'], function(Deferred, all) {
// code to initate query tasks here
var deferredTask1 = new Deferred();
queryTask1.execute(query1);
queryTask1.on("complete", function(evt1) {
deferredTask1.resolve(evt1);
});
var deferredTask2 = new Deferred();
queryTask2.execute(query1);
queryTask2.on("complete", function(evt1) {
deferredTask2.resolve(evt1);
});
all([deferredTask1, deferredTask1]).then(function(results) {
// results will be an Array
});
// -- or --
all({
deferredTask1: deferredTask1,
deferredTask2: deferredTask2
}).then(function(results) {
// results will be an Object using the keys "promise1" and "promise2"
});
});