<?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 Flex developers, embrace Promise! in ArcGIS API for Flex Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-flex-questions/flex-developers-embrace-promise/m-p/421614#M9884</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi to all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;switching back and forth between javascript and flex (actually AS), I found that the most useful and fantastic pattern that is raising in the JS world are Promises.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Promises are a programming construct that have been around since 1976, and even Dojo have a promise package: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://dojotoolkit.org/reference-guide/1.9/dojo/promise.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://dojotoolkit.org/reference-guide/1.9/dojo/promise.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In short, a promise represents a value that is not yet known and a deferred represents work that is not yet finished.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The beauty is that you can concatenate your functions like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
queryForGeometry()
&amp;nbsp; .then(fillGeometry)
&amp;nbsp; .then(queryForKeygis)
&amp;nbsp; .then(fillKeygis)
&amp;nbsp; .then(completeUpdate)
&amp;nbsp; .done();
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;After some research I've found this library: &lt;/SPAN&gt;&lt;A href="https://github.com/CodeCatalyst/promise-as3" rel="nofollow noopener noreferrer" target="_blank"&gt;https://github.com/CodeCatalyst/promise-as3&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And finally I can say goodbye to nested callbacks! And with only one line of code you can adapt your query and identify to use promise!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
// Somewhere in your code, at the load of the Application
Promise.registerAdapter( AsyncTokenAdapter.adapt );

var task:QueryTask = new QueryTask(Endpoint.QUERY_LAYER_FOR_BUILDING);
var query:Query = new Query();
query.where = "1=1";
query.outFields = ["*"];
// query initialisation ...
Promise.when(task.execute(query)).then(doSomethingWithQueryResult);

function doSomethingWithQueryResult(featureSet:FeatureSet):void {
 ...
}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With only one query is really simple and maybe not so revolutionary, but when you have more query that depends on other query this pattern simplify a lot my code and it's much simple to read.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The functions are all on the same level, executed in sequence and no more nested!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
function queryForGeometry():Promise {
&amp;nbsp; ...
}
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
function fillGeometry(featureSet:FeatureSet, token:Object = null):Graphic {
&amp;nbsp; ...
}
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
function queryForKeygis(feature:Graphic):Promise {
&amp;nbsp; ...
}
&amp;nbsp;&amp;nbsp; 
function fillKeygis(featureSet:FeatureSet, token:Object = null):Object {
&amp;nbsp; ...
}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can even do some thing like this, call a webservice, receive the result, the execute a query with some data...easy!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
public class Http {
&amp;nbsp; public static function get(uri:String):Promise {
&amp;nbsp;&amp;nbsp;&amp;nbsp; var service:HTTPService = new HTTPService();
&amp;nbsp;&amp;nbsp;&amp;nbsp; service.url = encodeURI(uri);
&amp;nbsp;&amp;nbsp;&amp;nbsp; var header:Object = new Object();
&amp;nbsp;&amp;nbsp;&amp;nbsp; header["Accept"] = "application/json; charset=utf-8";
&amp;nbsp;&amp;nbsp;&amp;nbsp; service.headers = header;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return Promise.when(service.send());
&amp;nbsp; }
}

// In another class, some function to query

Http.get("http://.../streets/1").then(parseJson).then(queryGISLayerWithId).done();
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Happy Coding!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 19:02:31 GMT</pubDate>
    <dc:creator>LucaSimone</dc:creator>
    <dc:date>2021-12-11T19:02:31Z</dc:date>
    <item>
      <title>Flex developers, embrace Promise!</title>
      <link>https://community.esri.com/t5/arcgis-api-for-flex-questions/flex-developers-embrace-promise/m-p/421614#M9884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi to all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;switching back and forth between javascript and flex (actually AS), I found that the most useful and fantastic pattern that is raising in the JS world are Promises.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Promises are a programming construct that have been around since 1976, and even Dojo have a promise package: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://dojotoolkit.org/reference-guide/1.9/dojo/promise.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://dojotoolkit.org/reference-guide/1.9/dojo/promise.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In short, a promise represents a value that is not yet known and a deferred represents work that is not yet finished.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The beauty is that you can concatenate your functions like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
queryForGeometry()
&amp;nbsp; .then(fillGeometry)
&amp;nbsp; .then(queryForKeygis)
&amp;nbsp; .then(fillKeygis)
&amp;nbsp; .then(completeUpdate)
&amp;nbsp; .done();
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;After some research I've found this library: &lt;/SPAN&gt;&lt;A href="https://github.com/CodeCatalyst/promise-as3" rel="nofollow noopener noreferrer" target="_blank"&gt;https://github.com/CodeCatalyst/promise-as3&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And finally I can say goodbye to nested callbacks! And with only one line of code you can adapt your query and identify to use promise!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
// Somewhere in your code, at the load of the Application
Promise.registerAdapter( AsyncTokenAdapter.adapt );

var task:QueryTask = new QueryTask(Endpoint.QUERY_LAYER_FOR_BUILDING);
var query:Query = new Query();
query.where = "1=1";
query.outFields = ["*"];
// query initialisation ...
Promise.when(task.execute(query)).then(doSomethingWithQueryResult);

function doSomethingWithQueryResult(featureSet:FeatureSet):void {
 ...
}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With only one query is really simple and maybe not so revolutionary, but when you have more query that depends on other query this pattern simplify a lot my code and it's much simple to read.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The functions are all on the same level, executed in sequence and no more nested!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
function queryForGeometry():Promise {
&amp;nbsp; ...
}
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
function fillGeometry(featureSet:FeatureSet, token:Object = null):Graphic {
&amp;nbsp; ...
}
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
function queryForKeygis(feature:Graphic):Promise {
&amp;nbsp; ...
}
&amp;nbsp;&amp;nbsp; 
function fillKeygis(featureSet:FeatureSet, token:Object = null):Object {
&amp;nbsp; ...
}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can even do some thing like this, call a webservice, receive the result, the execute a query with some data...easy!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
public class Http {
&amp;nbsp; public static function get(uri:String):Promise {
&amp;nbsp;&amp;nbsp;&amp;nbsp; var service:HTTPService = new HTTPService();
&amp;nbsp;&amp;nbsp;&amp;nbsp; service.url = encodeURI(uri);
&amp;nbsp;&amp;nbsp;&amp;nbsp; var header:Object = new Object();
&amp;nbsp;&amp;nbsp;&amp;nbsp; header["Accept"] = "application/json; charset=utf-8";
&amp;nbsp;&amp;nbsp;&amp;nbsp; service.headers = header;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return Promise.when(service.send());
&amp;nbsp; }
}

// In another class, some function to query

Http.get("http://.../streets/1").then(parseJson).then(queryGISLayerWithId).done();
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Happy Coding!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:02:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-flex-questions/flex-developers-embrace-promise/m-p/421614#M9884</guid>
      <dc:creator>LucaSimone</dc:creator>
      <dc:date>2021-12-11T19:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: Flex developers, embrace Promise!</title>
      <link>https://community.esri.com/t5/arcgis-api-for-flex-questions/flex-developers-embrace-promise/m-p/421615#M9885</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the great tip!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Mar 2014 16:57:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-flex-questions/flex-developers-embrace-promise/m-p/421615#M9885</guid>
      <dc:creator>DasaPaddock</dc:creator>
      <dc:date>2014-03-27T16:57:39Z</dc:date>
    </item>
  </channel>
</rss>

