Introduction
Hello world
https://promisesaplus.com/
http://spring.io/understanding/javascript-promises
http://www.html5rocks.com/en/tutorials/es6/promises/
You need to make 'ordinary' components Promise-ready.
Main.xml (package: default package)
Code Block |
---|
theme | Eclipse |
---|
language | actionscript3 |
---|
linenumbers | true |
---|
|
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600"
creationComplete="creationCompleteHandler(event)">
<fx:Script><![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.http.HTTPService;
import nl.ixms.HTTPServicePromise;
import org.apache.flex.promises.interfaces.IThenable;
private var httpServicePromise_:HTTPServicePromise;
private function btnClickHandler(event:MouseEvent):void
{
httpServicePromise_.sendWithPromise().
then(useFirstResultInAnotherPromise).
then(showSecondResultInAlert);
}
private function creationCompleteHandler(event:FlexEvent):void
{
httpServicePromise_ = new HTTPServicePromise();
httpServicePromise_.url = 'http://people.apache.org/~erikdebruin/promise.txt';
httpServicePromise_.method = 'GET';
httpServicePromise_.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
}
private function showSecondResultInAlert(result:String):void
{
Alert.show(result);
}
private function useFirstResultInAnotherPromise(result:String):IThenable
{
var url:String = result.split('\n')[3];
var anotherHTTPServicePromise:HTTPServicePromise = new HTTPServicePromise();
anotherHTTPServicePromise.url = url;
anotherHTTPServicePromise.method = 'GET';
anotherHTTPServicePromise.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
return anotherHTTPServicePromise.sendWithPromise();
}
]]></fx:Script>
<fx:Declarations />
<s:VGroup>
<s:Button label="Start the promises chain" click="btnClickHandler(event)"/>
</s:VGroup>
</s:Application> |
HTTPServicePromise.as (package: nl.ixms)
Code Block |
---|
theme | Eclipse |
---|
language | actionscript3 |
---|
linenumbers | true |
---|
|
package nl.ixms
{
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import org.apache.flex.promises.Promise;
import org.apache.flex.promises.interfaces.IThenable;
public class HTTPServicePromise extends HTTPService
{
public function HTTPServicePromise(rootURL:String=null, destination:String=null)
{
super(rootURL, destination);
}
public function sendWithPromise(parameters:Object=null):IThenable
{
return new Promise(function (fulfill:Function = null,
reject:Function = null):* {
/* Link the service result events with the promise handlers */
addEventListener(ResultEvent.RESULT, function (event:ResultEvent):void {
fulfill(event.result);
});
addEventListener(FaultEvent.FAULT, function (event:FaultEvent):void {
reject(event.message);
});
send(parameters);
});
}
}
} |