Index: src/promise.js |
diff --git a/src/promise.js b/src/promise.js |
index 552ce3df30a6ae2cf0d7e2f78195a58334972cd1..db7863f8098f686cab2fc4545eab1429165797fc 100644 |
--- a/src/promise.js |
+++ b/src/promise.js |
@@ -173,29 +173,37 @@ function PromiseCatch(onReject) { |
} |
function PromiseEnqueue(value, tasks) { |
- RunMicrotasks.queue.push(function() { |
- for (var i = 0; i < tasks.length; i += 2) { |
- PromiseHandle(value, tasks[i], tasks[i + 1]) |
- } |
- }); |
- |
+ promiseEvents.push(value, tasks); |
%SetMicrotaskPending(true); |
} |
-function PromiseHandle(value, handler, deferred) { |
- try { |
- var result = handler(value); |
- if (result === deferred.promise) |
- throw MakeTypeError('promise_cyclic', [result]); |
- else if (IsPromise(result)) |
- result.chain(deferred.resolve, deferred.reject); |
- else |
- deferred.resolve(result); |
- } catch(e) { |
- // TODO(rossberg): perhaps log uncaught exceptions below. |
- try { deferred.reject(e) } catch(e) {} |
+function PromiseMicrotaskRunner() { |
+ var events = promiseEvents; |
+ if (events.length > 0) { |
+ promiseEvents = new InternalArray; |
+ for (var i = 0; i < events.length; i += 2) { |
+ var value = events[i]; |
+ var tasks = events[i + 1]; |
+ for (var j = 0; j < tasks.length; j += 2) { |
+ var handler = tasks[j]; |
+ var deferred = tasks[j + 1]; |
+ try { |
+ var result = handler(value); |
+ if (result === deferred.promise) |
+ throw MakeTypeError('promise_cyclic', [result]); |
+ else if (IsPromise(result)) |
+ result.chain(deferred.resolve, deferred.reject); |
+ else |
+ deferred.resolve(result); |
+ } catch(e) { |
+ // TODO(rossberg): perhaps log uncaught exceptions below. |
+ try { deferred.reject(e) } catch(e) {} |
+ } |
+ } |
+ } |
} |
} |
+RunMicrotasks.runners.push(PromiseMicrotaskRunner); |
// Multi-unwrapped chaining with thenable coercion. |