Skip to content

Instantly share code, notes, and snippets.

@Stray
Created February 24, 2012 16:50
Show Gist options
  • Save Stray/1901979 to your computer and use it in GitHub Desktop.
Save Stray/1901979 to your computer and use it in GitHub Desktop.
Example of promise
package modules.common.net
{
import flash.display.DisplayObject;
import org.osflash.signals.Signal;
public class ResourcePromise implements IPromise, IResourcePromise
{
protected var _result:DisplayObject;
protected var _errorMessage:String;
protected var _loaded:Signal = new Signal(DisplayObject);
protected var _error:Signal = new Signal(String);
protected var _resource:Resource;
public function ResourcePromise(resource:Resource)
{
_resource = resource;
}
public function get resource():Resource
{
return _resource;
}
public function fulfill(content:DisplayObject):void
{
_result = content;
_loaded.dispatch(_result);
}
public function handleError(errorMessage:String):void
{
_errorMessage = errorMessage;
_error.dispatch(_errorMessage);
}
public function onLoaded(handler:Function):IPromise
{
_loaded.addOnce(handler);
if(_result)
handler(_result);
return this;
}
public function onError(handler:Function):IPromise
{
_error.add(handler);
if(_errorMessage)
handler(_errorMessage);
return this;
}
public function clear():void
{
_loaded.removeAll();
_error.removeAll();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment