I have this issue that I tried with promises and async/await to pause each iteration of the loop until a function completes its task with an esrirequest.
Here is an example. Before I get the response (#23) the loop proceed to the next iteration.
function loop() {
var variable =null
for (let j = 0; j < records.length; j++) {
let thestate = records[j];
get_results(thestate);
if (variable){
//extra work
}
}
async function get_results(state) {
let getinfo =
"https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2/query?f=json&Where=STATE_NAME='";
params = state;
params += "'&outFields=NAME,HISPANIC";
getinfo = getinfo + encodeURI(params);
var options = {
query: {
f: "json",
},
responseType: "json",
};
await esriRequest(getinfo, options).then((response) => {
console.log(response);
variable = response.data.var
});
}