<?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 Getting Max Value using SummaryStatistics in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1277525#M80837</link>
    <description>&lt;P&gt;Hi, so I'm trying to find the maximum value for a field using summary Statistics however im getting an undefined value when trying to do console.log()&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var maxVAL=0;
summaryStatistics({
  layer: fieldlayer,
  field: "fieldName",
}).then(function(stats){
  maxVAL= stats.max;
  // console.log(maxVAL) // works here
});
console.log(maxVAL)&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 11 Apr 2023 20:08:03 GMT</pubDate>
    <dc:creator>TheGamer</dc:creator>
    <dc:date>2023-04-11T20:08:03Z</dc:date>
    <item>
      <title>Getting Max Value using SummaryStatistics</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1277525#M80837</link>
      <description>&lt;P&gt;Hi, so I'm trying to find the maximum value for a field using summary Statistics however im getting an undefined value when trying to do console.log()&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var maxVAL=0;
summaryStatistics({
  layer: fieldlayer,
  field: "fieldName",
}).then(function(stats){
  maxVAL= stats.max;
  // console.log(maxVAL) // works here
});
console.log(maxVAL)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 11 Apr 2023 20:08:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1277525#M80837</guid>
      <dc:creator>TheGamer</dc:creator>
      <dc:date>2023-04-11T20:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Max Value using SummaryStatistics</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1277622#M80839</link>
      <description>&lt;P&gt;Because the summaryStatistic is async, the script will call summaryStatistic() and then immediately run the next line of code which is your bottom console.log(maxVal).&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;But because the summaryStatistic hasn't actually finished yet maxVal will return 0.&lt;BR /&gt;The .then() only runs once summaryStatistic has finished which is why the console.log inside that function returns the correct result.&lt;BR /&gt;&lt;BR /&gt;Javascript introduced async/await to help write cleaner code that is easier to read.&lt;BR /&gt;await means - wait for the function to return before continuing onto the next line.&lt;BR /&gt;&lt;BR /&gt;Using await your code would look like:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;async function() {
let maxVAL=0;
const stats = await summaryStatistics({
  layer: fieldlayer,
  field: "fieldName",
});

  maxVAL= stats.max;
  console.log(maxVAL);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 01:22:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1277622#M80839</guid>
      <dc:creator>JamesIng</dc:creator>
      <dc:date>2023-04-12T01:22:49Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Max Value using SummaryStatistics</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1277788#M80844</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/666601"&gt;@JamesIng&lt;/a&gt;&amp;nbsp;I tried to use maxVAL outside the async function() and im getting promise pending&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let maxVAL=0;
async function asynCall() {
const stats = await summaryStatistics({
  layer: fieldlayer,
  field: "fieldName",
});
  return Promise.resolve(stats.max);
}

maxVAL = asynCall()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 13:49:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1277788#M80844</guid>
      <dc:creator>TheGamer</dc:creator>
      <dc:date>2023-04-12T13:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Max Value using SummaryStatistics</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1279444#M80904</link>
      <description>&lt;P&gt;Cool you're real close, with async/await it essentially replaces the classic Javascript promises.&lt;BR /&gt;&lt;BR /&gt;In your case you should be able to tweak return stats directly from asyncCall() and await the results&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;So would look like something like:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;async function asynCall() {
const stats = await summaryStatistics({
  layer: fieldlayer,
  field: "fieldName",
});
  return stats.max;
}

let maxVAL = 0;
maxVal = await asynCall()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Another great thing to look at is in es6 you can now do whats known as 'destructuring' so you can actually reference a property of an object directly.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;In your case we would tweak the above into:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;async function asynCall() {
const stats = await summaryStatistics({
  layer: fieldlayer,
  field: "fieldName",
});
 return stats;
}

let { max }= await asynCall()&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Apr 2023 20:51:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/getting-max-value-using-summarystatistics/m-p/1279444#M80904</guid>
      <dc:creator>JamesIng</dc:creator>
      <dc:date>2023-04-17T20:51:48Z</dc:date>
    </item>
  </channel>
</rss>

