<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: pause loop for a function to return a result from esrirequest in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388451#M83796</link>
    <description>&lt;P&gt;Thank you Jeff. However it does not do the intended purpose. I wanted each iteration to go through the esri request and do the calculations based on the response before it proceeds to the next iteration. The script provided, it goes through the sStates loop and then execute all promises in parallel (Promise.all). As I mentioned before, I wanted each iteration to run in series to grab the variable at #27 and uses at #6 before it proceeds to the next iteration.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Feb 2024 00:06:28 GMT</pubDate>
    <dc:creator>LefterisKoumis</dc:creator>
    <dc:date>2024-02-29T00:06:28Z</dc:date>
    <item>
      <title>pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388164#M83787</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example. Before I get the response (#23) the loop proceed to the next iteration.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function loop() {
  var variable =null
  for (let j = 0; j &amp;lt; 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&amp;amp;Where=STATE_NAME='";
  params = state;
  params += "'&amp;amp;outFields=NAME,HISPANIC";

  getinfo = getinfo + encodeURI(params);

  var options = {
    query: {
      f: "json",
    },
    responseType: "json",
  };
  await esriRequest(getinfo, options).then((response) =&amp;gt; {
    console.log(response);
    variable = response.data.var
  
    
  });
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 19:33:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388164#M83787</guid>
      <dc:creator>LefterisKoumis</dc:creator>
      <dc:date>2024-02-28T19:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388180#M83788</link>
      <description>&lt;P&gt;There is a &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of" target="_self"&gt;for await...of&lt;/A&gt; loop that you can use in this case.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 17:59:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388180#M83788</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2024-02-28T17:59:33Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388225#M83790</link>
      <description>&lt;P&gt;I'm not sure what you need to set an await for, just handling the Esri Request response should be sufficient, as in:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function get_results(state) {
  let getinfo =
  "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2/query?f=json&amp;amp;Where=STATE_NAME='";
  params = state;
  params += "'&amp;amp;outFields=NAME,HISPANIC";

  getinfo = getinfo + encodeURI(params);
  esriRequest(getinfo, {
	responseType: "json"options
	}).then(function (response) {
		// Do something like
		let thisVal = response.data.fields[0].domain.codedValues;
		optionsSelector.push({value: thisVal, label thisVal.name});
		console.log(response);
	}).catch(function (error) {
		console.error("esriRequest Error: " + error);
	});
  });
}&lt;/LI-CODE&gt;&lt;P&gt;But if you need to wait for something you can always push that into a promise, like:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function get_results(state) {
	let defStateQuery = new Deferred();
	let promiseQueries = []
	promiseQueries.push(defStateQuery)
	setTimeout(function (inState) {
		let getinfo = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2/query?f=json&amp;amp;Where=STATE_NAME='";
  	params = inState;
  			params += "'&amp;amp;outFields=NAME,HISPANIC";

  		getinfo = getinfo + encodeURI(params);

  		//var options = { query: {f: "json",}, responseType: "json"};

  		esriRequest(getinfo, {
			responseType: "json"options
			}).then(function (response) {
				// Do something like
				let thisVal = response.data.fields[0].domain.codedValues;
				optionsSelector.push({value: thisVal, label thisVal.name});
				console.log(response);
				deferred.resolve(response);
                        	return deferred.promise
			}).catch(function (error) {
				console.error("esriRequest Error: " + error);
				deferred.reject(error);
                    		return deferred.promise;
			});
  		});
	}, 0);
	return deferred.promise;
}

all(promiseQueries).then(function (results) {
	console.log(results);
});&lt;/LI-CODE&gt;&lt;P&gt;This sample allows you to enter a URL and see the results, but the REST service seems to not be working (maybe there's too many of us trying to write a response, LOL).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A title="Esri Request Sample" href="https://developers.arcgis.com/javascript/latest/sample-code/request/" target="_blank" rel="noopener"&gt;https://developers.arcgis.com/javascript/latest/sample-code/request/&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 19:12:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388225#M83790</guid>
      <dc:creator>JeffreyWilkerson</dc:creator>
      <dc:date>2024-02-28T19:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388232#M83791</link>
      <description>&lt;P&gt;THank you Ken. I don't think in my case it will work. I didn't explain my issue well. I changed my script.&amp;nbsp;&lt;/P&gt;&lt;P&gt;THe esriRequest will produce a value (#27) which it will be used on #6 before the next iteration.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 19:36:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388232#M83791</guid>
      <dc:creator>LefterisKoumis</dc:creator>
      <dc:date>2024-02-28T19:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388237#M83792</link>
      <description>&lt;P&gt;Thank you Jeffrey.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I modified my script to explain better.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The esriRequest will produce a value (#27) which it will be used on #6 before the next iteration.&lt;/P&gt;&lt;P&gt;The script presently will not pause the loop awaiting the processing of #6 in my script.&lt;/P&gt;&lt;P&gt;Also in your script, I assume that there is no option on&amp;nbsp; &amp;nbsp;#15 (second script) since you define options in esriRequest.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 19:42:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388237#M83792</guid>
      <dc:creator>LefterisKoumis</dc:creator>
      <dc:date>2024-02-28T19:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388341#M83793</link>
      <description>&lt;P&gt;I went back and looked what I had done for 4.24, and it relied on Dojo promises. Esri just talks about the .then/.catch method of working with promises, but in a script I had developed I as passing the promise (deferred) among multiple functions and only after successfully completing the last one did I resolve (or reject) the promise. Then, the program waited for all function calls to finish for all records, which as accomplished using the Dojo 'All' function. Looks like this is not available anymore as 4.29 now seems devoid of all Dojo, and after looking at possibly adding it to the libraries, I thought the better of it and decided I should figure out how to do this with native Javascript promises.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This may not be the correct way, but it works. You just have to parse through all of the resulting features for each call to get (in this case) the value for 'Hispanic':&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /&amp;gt;
    &amp;lt;title&amp;gt;Request data from a remote server | Sample | ArcGIS Maps SDK for JavaScript 4.29&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.29/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.29/"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script type="module" src="https://js.arcgis.com/calcite-components/2.5.1/calcite.esm.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/2.5.1/calcite.css" /&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;calcite-shell&amp;gt;
      &amp;lt;calcite-panel heading="Using esri/request"&amp;gt;
        &amp;lt;calcite-block
          heading="Enter a URL:"
          description='Enter a server URL to the input box below, then click the "Make Request" button to send a request.'
          open
        &amp;gt;
          &amp;lt;calcite-input-text
            id="input-url"
            placeholder="https://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer"
            value="https://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer"
            required
          &amp;gt;&amp;lt;/calcite-input-text&amp;gt;
          &amp;lt;calcite-button id="request-button" style="padding-top: 5px"&amp;gt;Make Request&amp;lt;/calcite-button&amp;gt;
          &amp;lt;calcite-label style="padding-top: 10px" scale="l"
            &amp;gt;Server response from the request:
            &amp;lt;calcite-text-area
              id="results-text-area"
              placeholder="Request response will show here.."
              read-only
              rows="25"
            &amp;gt;&amp;lt;/calcite-text-area&amp;gt;
          &amp;lt;/calcite-label&amp;gt;
        &amp;lt;/calcite-block&amp;gt;
      &amp;lt;/calcite-panel&amp;gt;
    &amp;lt;/calcite-shell&amp;gt;
    &amp;lt;calcite-alert id="alert" kind="danger" icon label="Danger alert" auto-close&amp;gt;
      &amp;lt;div slot="title"&amp;gt;Enter a valid URL.&amp;lt;/div&amp;gt;
    &amp;lt;/calcite-alert&amp;gt;
    &amp;lt;script&amp;gt;
      require(["esri/request"], (esriRequest) =&amp;gt; {
        const requestButton = document.getElementById("request-button");
        const urlInput = document.getElementById("input-url");
        const textArea = document.getElementById("results-text-area");
        const alert = document.getElementById("alert");
        let url;

        let sStates = ['Arizona', 'California', 'Nevada']
        let promiseStates = [];
        let sResults = '';
        
        requestButton.addEventListener("click", () =&amp;gt; {
            //var variable = null;
            for (let j = 0; j &amp;lt; sStates.length; j++) {
              let thestate = sStates[j];
              const aDef = new Promise((resolve, reject) =&amp;gt; {
                get_results(thestate, resolve, reject);
              });
              promiseStates.push(aDef);
            }
            
            Promise.all(promiseStates).then((values) =&amp;gt; {
                console.log("in Promise.all");
                console.log(values);
                textArea.value += sResults;
            });
        });
        
        async function get_results(state, pRes, pRej) {
            console.log(state);
          let getinfo =
            "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2/query?f=json&amp;amp;Where=STATE_NAME='";
          params = state;
          params += "'&amp;amp;outFields=NAME,HISPANIC";

          getinfo = getinfo + encodeURI(params);

          var options = {
            query: {
              f: "json",
            },
            responseType: "json",
          };
          console.log(getinfo);
          console.log()
          await esriRequest(getinfo, options).then((response) =&amp;gt; {
            console.log("in esriRequest");
            console.log(response.data);
            sResults += response.data.fields["Name"];
            return pRes(response);
          })
            .catch(function (error) {
		            console.error("esriRequest Error: " + error);
		            textArea.value = error;
                return pRej(error);
	          });
        }
        
        

      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;(oh, and it uses your original function for calling the REST service).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 21:40:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388341#M83793</guid>
      <dc:creator>JeffreyWilkerson</dc:creator>
      <dc:date>2024-02-28T21:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388346#M83794</link>
      <description>&lt;P&gt;Oh, and look at the Console for the results. I just used the Esri sample for promises, but I cleaned most of it out and never got the textArea to work well. But the data's viewable in the console.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 21:45:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388346#M83794</guid>
      <dc:creator>JeffreyWilkerson</dc:creator>
      <dc:date>2024-02-28T21:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388451#M83796</link>
      <description>&lt;P&gt;Thank you Jeff. However it does not do the intended purpose. I wanted each iteration to go through the esri request and do the calculations based on the response before it proceeds to the next iteration. The script provided, it goes through the sStates loop and then execute all promises in parallel (Promise.all). As I mentioned before, I wanted each iteration to run in series to grab the variable at #27 and uses at #6 before it proceeds to the next iteration.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 00:06:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388451#M83796</guid>
      <dc:creator>LefterisKoumis</dc:creator>
      <dc:date>2024-02-29T00:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388464#M83797</link>
      <description>&lt;P&gt;If you make your loop async, I think you can get the behavior you're looking for.&lt;/P&gt;&lt;P&gt;&lt;A href="https://codepen.io/odoe/pen/MWRWwax?editors=0010" target="_blank"&gt;https://codepen.io/odoe/pen/MWRWwax?editors=0010&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If I was doing this from scratch, I might set it up a bit different, so that the variable was a returned value and not set on each iteration, but this seems to work.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 00:38:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1388464#M83797</guid>
      <dc:creator>ReneRubalcava</dc:creator>
      <dc:date>2024-02-29T00:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: pause loop for a function to return a result from esrirequest</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1389108#M83807</link>
      <description>&lt;P&gt;Thank you. Huge oversight on my part not to add another async/wait at the beginning.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 20:48:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/pause-loop-for-a-function-to-return-a-result-from/m-p/1389108#M83807</guid>
      <dc:creator>LefterisKoumis</dc:creator>
      <dc:date>2024-02-29T20:48:22Z</dc:date>
    </item>
  </channel>
</rss>

