CC-Archived / promise-as3

Promises/A+ compliant implementation in ActionScript 3.0
167 stars 52 forks source link

Consequences aren't executed asynchronously #54

Open PierrotLL opened 7 years ago

PierrotLL commented 7 years ago

In Consequence.execute, callbacks are executed one after the other. But if one of them adds a new callback in the queue, this new one will be executed during the same frame. Repeat the same operation again and again, then you obtain an infinite loop that ends in error 1502.

The promises standards well say that onFulfilled should be executed the frame after deferred.resolve.

How to fix : Use 2 callback queues, and do double-buffering. While the first queue is executed, new callbacks are pushed into the second queue, then the second queue is executed the next frame.

PierrotLL commented 7 years ago

Example:

var frameId:int = 0;
addEventListener(Event.ENTER_FRAME, function () : void {
    frameId++;
});

function foo() : Promise {
    trace(frameId);
    var d:Deferred = new Deferred();
    d.resolve(null);
    return d.promise.then(foo);
}
foo();