Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using ArcGIS JS API version 3.6. I want to make query task synchronize. While executing query task reaming code will wait until completion of query task.

For example I have shared my code here which contains query layer.

Case 1: In which I am directly executing the code and printing the log in console window, where you can see before the query task output is displaying

Case 2: In this case we have used alert (for time delay) in the code then value is getting before query task.

-Researching on Google and going through similar question

Case 1 screenshot : -Screen shot for case 1

Case 2 screenshot : -Screen shot for case 2

Any help will be great !!! thanks in advance : )

share|improve this question
2  
Querytask is always asynchronous. in your code, you have: queryTask.execute(query, function(featureSet){ }); this function is called only when the request is complete and our code gets the response from the server. XHttpRequests are asyncronous by design, and if you want some code to be called after you have the response, you should call that code from this function –  Devdatta Tengshe Jun 19 at 12:08
    
@Devdatta Tengshe thanks for your reply. I will check and let you know.. –  Sunil Jun 19 at 15:38
    
seems like a duplicate of this question gis.stackexchange.com/questions/95730/… –  John Gravois Jun 19 at 16:29
add comment

1 Answer 1

up vote 2 down vote accepted

It is exactly as @Devdatta Tengshe already said so this is just to expand. Any code you want to execute only after the queryTask has returned simply goes in the function you've already defined. So, you can just move your console log statement to go in there after you've filled the array:

queryTask.execute(query, function(featureSet){
    for (var x = 0; x < featureSet.features.length; x++) {
       dataArray.push(featureSet.features[x].attributes.SYMBOLNAME);
    }
    console.log("dataArray=="+dataArray.length);
});

You can also define a second function (so the third parameter to queryTask that is called if queryTask returns an error instead of success:

queryTask.execute(query, function(featureSet){
    // stuff to do on success
}, function(error){
    // stuff to do on error
}

You don't have to declare the functions in the queryTask if you want to organise you code differently. You can declare them in the main body of your JavaScript and then just call them by name:

// code to prepare your query task here
...

// execute your query passing it the callback functions
queryTask.execute(query, querySuccess, queryError);

function querySuccess(featureSet) {
    // stuff to do on success
}

function queryError(error) {
    // stuff to do on error
}
share|improve this answer
    
Thanks for the detail information. Its work for me : ) thanks a lot !! –  Sunil Jun 23 at 4:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.