dojo / promise / all可以一起用于xhr请求和arcgis querytask吗?

时间:2018-10-23 02:23:41

标签: promise dojo arcgis

我有一个普通的XHR请求和json响应。我还有另一个arcgis querytask要执行。

dojo / promise / all可以用于以上两个异步请求来处理所有返回的响应吗?如果是,该怎么办?

1 个答案:

答案 0 :(得分:0)

是的。 首先,应该通过esri / request创建XHR对象。返回的对象是promise。 queryTask对象是promise。

代码如下:

<script>
    require(['dojo/Deferred', 'dojo/promise/all',"esri/request", "esri/config", "esri/tasks/QueryTask","esri/tasks/query"], function(Deferred, all,esriRequest,esriConfig,QueryTask,Query) {
        // code to initate query tasks here
        var qURL = "https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5";

        var queryTask  = new QueryTask(qURL);

        var query  = new Query();
        query.returnGeometry = false;
        query.where = "STATE_NAME='California'";
        query.outFields = ["*"];
        esriConfig.defaults.io.corsEnabledServers.push("earthquake.usgs.gov");
        esriConfig.defaults.io.corsEnabledServers.push("https://sampleserver1.arcgisonline.com");

        var url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson";
        var requestHandle = esriRequest({
            "url":url
        });
        
        var deferredTask1 = new Deferred();


        queryTask.execute(query );
        queryTask.on("complete", function(evt1) {
            deferredTask1.resolve(evt1);
        });


        all([deferredTask1,  requestHandle]).then(function(results) {
            // results will be an Array
            var t=results;
            console.log("state fips =" +results[0].featureSet.features[0].attributes["STATE_FIPS"]);
            console.log("earthquake id=" +results[1].features[0].id);
        });


    });
</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title></title>

            <script src="https://js.arcgis.com/3.26/"></script>
        </head>
<body>


</body>
</html>