<?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 Process GeometryService.Project in Loop - Javascript in ArcGIS Server with JavaScript API Questions</title>
    <link>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/process-geometryservice-project-in-loop-javascript/m-p/863515#M638</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a situation where I need to process an array of points in a loop.&amp;nbsp; Within the loop I need to get the projected coordinates of the point - so I can do some additional processing.&amp;nbsp; However, the call to get the projected coordinates is an asynchronous call so the loop continues BEFORE I have my projected coordinates back from the server.&amp;nbsp; Can someone explain how to make this work?&amp;nbsp; Below is my code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;BR /&gt;&amp;lt;html&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;head&amp;gt;&lt;BR /&gt; &amp;lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&amp;gt;&lt;BR /&gt; &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10"&amp;gt;&lt;BR /&gt; &amp;lt;meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"&amp;gt;&lt;BR /&gt; &amp;lt;title&amp;gt;Test Callback&amp;lt;/title&amp;gt;&lt;BR /&gt;&lt;SPAN&gt; &amp;lt;link rel="stylesheet" href="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.26%2Fesri%2Fcss%2Fesri.css" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.26/esri/css/esri.css&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;style&amp;gt;&amp;lt;/style&amp;gt;&lt;BR /&gt;&lt;SPAN&gt; &amp;lt;script src="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.26%2F" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.26/&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&amp;lt;/script&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;script&amp;gt;&lt;BR /&gt; var map;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;require([&lt;BR /&gt; "esri/map",&lt;BR /&gt; "esri/geometry/Point",&lt;BR /&gt; "esri/tasks/GeometryService",&lt;BR /&gt; "esri/SpatialReference",&lt;BR /&gt; "esri/tasks/ProjectParameters",&lt;BR /&gt; "dojo/_base/array",&lt;BR /&gt; "dojo/dom",&lt;BR /&gt; "dojo/on",&lt;BR /&gt; "dojo/domReady!"],&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function(Map, Point, GeometryService, SpatialReference, ProjectParameters, arrayUtils, dom, on) { &lt;BR /&gt; var pointArray = [];&lt;BR /&gt; var newPointArray = [];&lt;BR /&gt; var myRunButton = dom.byId("runButton");&lt;BR /&gt; on(myRunButton, "click", runClick);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function runClick() {&lt;BR /&gt; alert("In runClick");&lt;BR /&gt; var baseX = 2572000;&lt;BR /&gt; var baseY = 600000;&lt;BR /&gt; var wkid = 2915;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// create an array of points&lt;BR /&gt; for (i=0; i &amp;lt; 3; i++) {&lt;BR /&gt; var thisPoint = new Point(baseX + (100 * i), baseY + (100 * i), new SpatialReference(Number(wkid)));&lt;BR /&gt; pointArray.push(thisPoint);&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// loop through the points and get the web mercator values&lt;BR /&gt; // I can't just send the whole point array over. The actual array in my code isn't a point array&lt;BR /&gt; // but the point is part of the process and I need its projected value before I proceed through the loop.&lt;BR /&gt; // And the points MUST remain in order.&lt;BR /&gt; arrayUtils.forEach(pointArray, function(thisPoint) {&lt;BR /&gt; // call method to convert the point&lt;BR /&gt; var webMercPT = getWebMercPt(thisPoint);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// here I want to take the results from the method getWebMercPt and do something with them &lt;BR /&gt; // however this code always fires before the pojection task is finished.&lt;BR /&gt; alert("in run click after call to getWebMercPt");&lt;BR /&gt; newPointArray.push(webMercPT);&lt;BR /&gt; });&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function getWebMercPt(pt) {&lt;BR /&gt;&lt;SPAN&gt; var geometryService = new GeometryService("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Futility.arcgisonline.com%2FArcGIS%2Frest%2Fservices%2FGeometry%2FGeometryServer" rel="nofollow" target="_blank"&gt;https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer&lt;/A&gt;&lt;SPAN&gt;");&lt;/SPAN&gt;&lt;BR /&gt; var params = new ProjectParameters();&lt;BR /&gt; params.geometries = [pt];&lt;BR /&gt; params.outSR = new SpatialReference(4326);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// this call needs to finish before returning to the calling method&lt;BR /&gt; // THIS IS THE PART I CAN'T FIGURE OUT - HOW DO YOU MAKE IT WAIT AND RETURN THE RESULTS FROM THE PROJECTION&lt;BR /&gt; geometryService.project(params).then(function(results) {&lt;BR /&gt; alert("In success");&lt;BR /&gt; return results;&lt;BR /&gt; }, errGetPoint);&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function processCoursePoint(results) {&lt;BR /&gt; //alert("Success: " + results.x.toString());&lt;BR /&gt; alert("In success");&lt;BR /&gt; return results;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function errGetPoint(e) {&lt;BR /&gt; alert("In error");&lt;BR /&gt; }&lt;BR /&gt; }); &lt;BR /&gt; &amp;lt;/script&amp;gt;&lt;BR /&gt;&amp;lt;/head&amp;gt;&lt;BR /&gt;&amp;lt;body&amp;gt;&lt;BR /&gt; &amp;lt;input type="button" id="runButton" value="Click Me" /&amp;gt;&lt;BR /&gt; &amp;lt;div id="results"&amp;gt;&amp;lt;/div&amp;gt;&lt;BR /&gt;&amp;lt;/body&amp;gt;&lt;BR /&gt;&amp;lt;/html&amp;gt;&lt;/P&gt;&lt;DIV style="color: #d4d4d4; background-color: #1e1e1e; font-weight: normal; font-size: 14px;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 27 Nov 2018 14:07:09 GMT</pubDate>
    <dc:creator>BryanLynn1</dc:creator>
    <dc:date>2018-11-27T14:07:09Z</dc:date>
    <item>
      <title>Process GeometryService.Project in Loop - Javascript</title>
      <link>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/process-geometryservice-project-in-loop-javascript/m-p/863515#M638</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a situation where I need to process an array of points in a loop.&amp;nbsp; Within the loop I need to get the projected coordinates of the point - so I can do some additional processing.&amp;nbsp; However, the call to get the projected coordinates is an asynchronous call so the loop continues BEFORE I have my projected coordinates back from the server.&amp;nbsp; Can someone explain how to make this work?&amp;nbsp; Below is my code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;BR /&gt;&amp;lt;html&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;head&amp;gt;&lt;BR /&gt; &amp;lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&amp;gt;&lt;BR /&gt; &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10"&amp;gt;&lt;BR /&gt; &amp;lt;meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"&amp;gt;&lt;BR /&gt; &amp;lt;title&amp;gt;Test Callback&amp;lt;/title&amp;gt;&lt;BR /&gt;&lt;SPAN&gt; &amp;lt;link rel="stylesheet" href="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.26%2Fesri%2Fcss%2Fesri.css" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.26/esri/css/esri.css&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;style&amp;gt;&amp;lt;/style&amp;gt;&lt;BR /&gt;&lt;SPAN&gt; &amp;lt;script src="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.26%2F" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.26/&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&amp;lt;/script&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;script&amp;gt;&lt;BR /&gt; var map;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;require([&lt;BR /&gt; "esri/map",&lt;BR /&gt; "esri/geometry/Point",&lt;BR /&gt; "esri/tasks/GeometryService",&lt;BR /&gt; "esri/SpatialReference",&lt;BR /&gt; "esri/tasks/ProjectParameters",&lt;BR /&gt; "dojo/_base/array",&lt;BR /&gt; "dojo/dom",&lt;BR /&gt; "dojo/on",&lt;BR /&gt; "dojo/domReady!"],&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function(Map, Point, GeometryService, SpatialReference, ProjectParameters, arrayUtils, dom, on) { &lt;BR /&gt; var pointArray = [];&lt;BR /&gt; var newPointArray = [];&lt;BR /&gt; var myRunButton = dom.byId("runButton");&lt;BR /&gt; on(myRunButton, "click", runClick);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function runClick() {&lt;BR /&gt; alert("In runClick");&lt;BR /&gt; var baseX = 2572000;&lt;BR /&gt; var baseY = 600000;&lt;BR /&gt; var wkid = 2915;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// create an array of points&lt;BR /&gt; for (i=0; i &amp;lt; 3; i++) {&lt;BR /&gt; var thisPoint = new Point(baseX + (100 * i), baseY + (100 * i), new SpatialReference(Number(wkid)));&lt;BR /&gt; pointArray.push(thisPoint);&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// loop through the points and get the web mercator values&lt;BR /&gt; // I can't just send the whole point array over. The actual array in my code isn't a point array&lt;BR /&gt; // but the point is part of the process and I need its projected value before I proceed through the loop.&lt;BR /&gt; // And the points MUST remain in order.&lt;BR /&gt; arrayUtils.forEach(pointArray, function(thisPoint) {&lt;BR /&gt; // call method to convert the point&lt;BR /&gt; var webMercPT = getWebMercPt(thisPoint);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// here I want to take the results from the method getWebMercPt and do something with them &lt;BR /&gt; // however this code always fires before the pojection task is finished.&lt;BR /&gt; alert("in run click after call to getWebMercPt");&lt;BR /&gt; newPointArray.push(webMercPT);&lt;BR /&gt; });&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function getWebMercPt(pt) {&lt;BR /&gt;&lt;SPAN&gt; var geometryService = new GeometryService("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Futility.arcgisonline.com%2FArcGIS%2Frest%2Fservices%2FGeometry%2FGeometryServer" rel="nofollow" target="_blank"&gt;https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer&lt;/A&gt;&lt;SPAN&gt;");&lt;/SPAN&gt;&lt;BR /&gt; var params = new ProjectParameters();&lt;BR /&gt; params.geometries = [pt];&lt;BR /&gt; params.outSR = new SpatialReference(4326);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// this call needs to finish before returning to the calling method&lt;BR /&gt; // THIS IS THE PART I CAN'T FIGURE OUT - HOW DO YOU MAKE IT WAIT AND RETURN THE RESULTS FROM THE PROJECTION&lt;BR /&gt; geometryService.project(params).then(function(results) {&lt;BR /&gt; alert("In success");&lt;BR /&gt; return results;&lt;BR /&gt; }, errGetPoint);&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function processCoursePoint(results) {&lt;BR /&gt; //alert("Success: " + results.x.toString());&lt;BR /&gt; alert("In success");&lt;BR /&gt; return results;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function errGetPoint(e) {&lt;BR /&gt; alert("In error");&lt;BR /&gt; }&lt;BR /&gt; }); &lt;BR /&gt; &amp;lt;/script&amp;gt;&lt;BR /&gt;&amp;lt;/head&amp;gt;&lt;BR /&gt;&amp;lt;body&amp;gt;&lt;BR /&gt; &amp;lt;input type="button" id="runButton" value="Click Me" /&amp;gt;&lt;BR /&gt; &amp;lt;div id="results"&amp;gt;&amp;lt;/div&amp;gt;&lt;BR /&gt;&amp;lt;/body&amp;gt;&lt;BR /&gt;&amp;lt;/html&amp;gt;&lt;/P&gt;&lt;DIV style="color: #d4d4d4; background-color: #1e1e1e; font-weight: normal; font-size: 14px;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Nov 2018 14:07:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-server-with-javascript-api-questions/process-geometryservice-project-in-loop-javascript/m-p/863515#M638</guid>
      <dc:creator>BryanLynn1</dc:creator>
      <dc:date>2018-11-27T14:07:09Z</dc:date>
    </item>
  </channel>
</rss>

