AnsweredAssumed Answered

Executing a series of queries inside a loop

Question asked by ndilekli on May 5, 2016
Latest reply on Apr 26, 2017 by josundwa



I am trying to loop through a string array of place names, build a query statement for each place, get the extent for the place, and build a bookmarks list. I am however baffled with how the results are built. Basically, it looks like while the query task is located inside the loop, it waits for the loop to finish to be executed. Therefore I get multiple and identical query results in the end. The core of the problem can be shown with this piece of code:

for (i = 1; i < allSysNames[i].length; i++) {

     query.where = "SysName =" + allSysNames[i];

     console.log("i inside the loop is: " + i);

     queryTask.executeForExtent(query, function(result){

          console.log("i inside query task equals to: " + i);



console.log("outside the loop");


The log is the following.

i inside the loop is: 1

i inside the loop is: 2

i inside the loop is: 3

outside the loop

i inside query task equals to 4

i inside query task equals to 4

i inside query task equals to 4


I am probably missing something very basic here, as I am new to Javascript and the API. What do I need to execute the queryTask for my purposes? Thank you!