<?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: How can I use a variable that is set in a function on an on event outside of the event? in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174027#M16141</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The question deals more with the progression of the code. Even though the line "console.log" is after the function section, it will get called first. The code inside that function will only occur when that event takes place (i.e. after the selection is complete on the feature layer).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 02 Sep 2015 13:10:22 GMT</pubDate>
    <dc:creator>KenBuja</dc:creator>
    <dc:date>2015-09-02T13:10:22Z</dc:date>
    <item>
      <title>How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174019#M16133</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For instance:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;featureLayer.on("selection-complete", function (result){&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var pPoly = result.features[0].geometry;&lt;/P&gt;&lt;P&gt;});&lt;/P&gt;&lt;P&gt;console.log(pPoly);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the console returns &lt;EM&gt;undefined&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Sep 2015 11:57:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174019#M16133</guid>
      <dc:creator>MichaelStranovsky</dc:creator>
      <dc:date>2015-09-02T11:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174020#M16134</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Michael,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; That when you use lang.hitch to maintain the scope of the function:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Be sure to require dojo/_base/lang&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
featureLayer.on("selection-complete", lang.hitch(this,function (result){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var pPoly = result.features[0].geometry;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Now to will have access to the external var inside this function
}));
console.log(pPoly);&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:59:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174020#M16134</guid>
      <dc:creator>RobertScheitlin__GISP</dc:creator>
      <dc:date>2021-12-11T08:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174021#M16135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Robert,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the quick response but I actually want to access the pPoly variable outside the onEvent after the function has been executed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Sep 2015 12:11:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174021#M16135</guid>
      <dc:creator>MichaelStranovsky</dc:creator>
      <dc:date>2015-09-02T12:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174022#M16136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can define the variable outside your function , but just so you know the console.log will still return undefined since it will run before your selection. To test you would have to put the console.log into your other "on" function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var pPoly;
featureLayer.on("selection-complete", function (result){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pPoly = result.features[0].geometry;
});
console.log(pPoly);&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:59:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174022#M16136</guid>
      <dc:creator>TimWitt2</dc:creator>
      <dc:date>2021-12-11T08:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174023#M16137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Michael,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Well think of it the same way now that you have access to the outside scope of the function you just set an outside var that you create to the pPoly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var pPoly;
featureLayer.on("selection-complete", lang.hitch(this,function (result){&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pPoly = result.features[0].geometry;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Now to will have access to the external var inside this function&amp;nbsp; 
}));&amp;nbsp; 
console.log(pPoly); &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:59:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174023#M16137</guid>
      <dc:creator>RobertScheitlin__GISP</dc:creator>
      <dc:date>2021-12-11T08:59:45Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174024#M16138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yeah I tried that and the console still reports undefined but if I place the console.log inside the event it return correctly.  I need to use it though outside the event.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Sep 2015 12:43:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174024#M16138</guid>
      <dc:creator>MichaelStranovsky</dc:creator>
      <dc:date>2015-09-02T12:43:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174025#M16139</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Michael,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; And you are sure you use the code I had in my last post with the lang.hitch and the var outside the function?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Sep 2015 12:58:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174025#M16139</guid>
      <dc:creator>RobertScheitlin__GISP</dc:creator>
      <dc:date>2015-09-02T12:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174026#M16140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is what I used:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;var pPoly;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;featureLayer.on("selection-complete", lang.hitch(this,function (result){&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;        pPoly = result.features[0].geometry;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;}));&lt;/P&gt;&lt;P&gt;console.log(pPoly);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Sep 2015 13:05:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174026#M16140</guid>
      <dc:creator>MichaelStranovsky</dc:creator>
      <dc:date>2015-09-02T13:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174027#M16141</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The question deals more with the progression of the code. Even though the line "console.log" is after the function section, it will get called first. The code inside that function will only occur when that event takes place (i.e. after the selection is complete on the feature layer).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Sep 2015 13:10:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174027#M16141</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2015-09-02T13:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174028#M16142</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think I understand why Micheal is having problem, the console is executed before the event is raised. The selectFeatures method is a deffered method. so you need to you something like this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var pPoly;

featureLayer.on("selection-complete", lang.hitch(this,function (result){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pPoly = result.features[0].geometry;
})).then(function(){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log(pPoly);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
});&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:59:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174028#M16142</guid>
      <dc:creator>thejuskambi</dc:creator>
      <dc:date>2021-12-11T08:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a variable that is set in a function on an on event outside of the event?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174029#M16143</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You are having scope and order of execution issues.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To do what you want, you need to do something like this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var pPoly;
function saySomething() {
&amp;nbsp; console.log(pPoly);
}
featureLayer.on("selection-complete", function (result) {
&amp;nbsp; pPoly = result.features[0].geometry;
&amp;nbsp; saySomething();
});&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dojo/on does not return a Promise, so you cannot use &lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;then&lt;/STRONG&gt;&lt;/SPAN&gt; with it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'd recommend you look over this blog post&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migration-blogpost/54816" target="_blank"&gt;Take your JavaScript Up a Notch&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And in particular look at &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="nofollow noopener noreferrer" target="_blank"&gt;closures&lt;/A&gt;. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:59:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-can-i-use-a-variable-that-is-set-in-a-function/m-p/174029#M16143</guid>
      <dc:creator>ReneRubalcava</dc:creator>
      <dc:date>2021-12-11T08:59:50Z</dc:date>
    </item>
  </channel>
</rss>

