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

Commit 9473371

Browse files
jbedardlgalfaso
authored andcommitted
perf($q): reduce closures when resolving promises
- changes Deferred.$$resolve to only wrap the internal resolve/reject when wrapping another promise Closes: #13293
1 parent e527559 commit 9473371

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
@@ -361,23 +349,34 @@ function qFactory(nextTick, exceptionHandler) {
361349
},
362350

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

383382
reject: function(reason) {

0 commit comments

Comments
 (0)