Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package async
{
public class Deferred implements Promise
{
private const pending:Array = [];
private var processed:Boolean;
private var completed:Boolean;
private var completionAction:String;
private var completionValue:*;
private var onResolved:Function;
private var onRejected:Function;
public function Deferred(onResolved:Function = null, onRejected:Function = null)
{
this.onResolved = onResolved;
this.onRejected = onRejected || throwError;
}
public function resolve(result:*):void
{
processed || process(onResolved, result);
}
public function reject(error:*):void
{
processed || process(onRejected, error);
}
public function then(onResolved:Function = null, onRejected:Function = null):Promise
{
if (onResolved || onRejected)
{
const deferred:Deferred = new Deferred(onResolved, onRejected);
NextTick.call(schedule, [deferred]);
return deferred;
}
return this;
}
private function throwError(error:*):void
{
throw error;
}
private function schedule(deferred:Deferred):void
{
pending.push(deferred);
completed && propagate();
}
private function propagate():void
{
for each (var deferred:Deferred in pending)
deferred[completionAction](completionValue);
pending.length = 0;
}
private function complete(action:String, value:*):void
{
onResolved = null;
onRejected = null;
completionAction = action;
completionValue = value;
completed = true;
propagate();
}
private function completeResolved(result:*):void
{
complete('resolve', result);
}
private function completeRejected(error:*):void
{
complete('reject', error);
}
private function process(closure:Function, value:*):void
{
processed = true;
try
{
closure && (value = closure(value));
value is Promise
? Promise(value).then(completeResolved, completeRejected)
: completeResolved(value);
}
catch (error:*)
{
completeRejected(error);
}
}
}
}
package async
{
import flash.display.Sprite;
import flash.events.Event;
public class NextTick
{
private static const SPR:Sprite = new Sprite();
private static const Q:Array = [];
public static function call(closure:Function, args:Array = null):void
{
Q.push(new Scope(closure, args));
Q.length == 1 && SPR.addEventListener(Event.ENTER_FRAME, run);
}
private static function run(e:Event):void
{
SPR.removeEventListener(Event.ENTER_FRAME, run);
for each (var scope:Scope in Q.splice(0))
scope.execute();
}
}
}
class Scope
{
private var _closure:Function;
private var _args:Array;
public function Scope(closure:Function, args:Array)
{
_closure = closure;
_args = args;
}
public function execute():void
{
_args ? _closure.apply(null, _args) : _closure();
}
}
package async
{
public interface Promise
{
function then(onResolved:Function = null, onRejected:Function = null):Promise;
}
}
@adammartin1981

Hi, I'm looking to use it in a commercial environment and wondered if like the promise.coffee from which it was ported, it could be licensed under MIT so that we could use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment