Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3621d92

Browse files
committed
perf($q): reduce closures when resolving promises
- changes Deferred.$$resolve to only wrap the internal resolve/reject when wrapping another promise
1 parent 63a834d commit 3621d92

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/ng/q.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,6 @@ function $$QProvider() {
243243
*/
244244
function qFactory(nextTick, exceptionHandler) {
245245
var $qMinErr = minErr('$q', TypeError);
246-
function callOnce(self, resolveFn, rejectFn) {
247-
var called = false;
248-
function wrap(fn) {
249-
return function(value) {
250-
if (called) return;
251-
called = true;
252-
fn.call(self, value);
253-
};
254-
}
255-
256-
return [wrap(resolveFn), wrap(rejectFn)];
257-
}
258246

259247
/**
260248
* @ngdoc method
@@ -362,23 +350,34 @@ function qFactory(nextTick, exceptionHandler) {
362350
},
363351

364352
$$resolve: function(val) {
365-
var then, fns;
366-
367-
fns = callOnce(this, this.$$resolve, this.$$reject);
353+
var then;
354+
var that = this;
355+
var done = false;
368356
try {
369357
if ((isObject(val) || isFunction(val))) then = val && val.then;
370358
if (isFunction(then)) {
371359
this.promise.$$state.status = -1;
372-
then.call(val, fns[0], fns[1], simpleBind(this, this.notify));
360+
then.call(val, resolvePromise, rejectPromise, simpleBind(this, this.notify));
373361
} else {
374362
this.promise.$$state.value = val;
375363
this.promise.$$state.status = 1;
376364
scheduleProcessQueue(this.promise.$$state);
377365
}
378366
} catch (e) {
379-
fns[1](e);
367+
rejectPromise(e);
380368
exceptionHandler(e);
381369
}
370+
371+
function resolvePromise(val) {
372+
if (done) return;
373+
done = true;
374+
that.$$resolve(val);
375+
}
376+
function rejectPromise(val) {
377+
if (done) return;
378+
done = true;
379+
that.$$reject(val);
380+
}
382381
},
383382

384383
reject: function(reason) {

0 commit comments

Comments
 (0)