Last active
December 11, 2015 00:49
-
-
Save darscan/4519372 to your computer and use it in GitHub Desktop.
Quick AS3 port of https://github.com/CodeCatalyst/promise.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package async | |
{ | |
public interface Promise | |
{ | |
function then(onResolved:Function = null, onRejected:Function = null):Promise; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?