Skip to content

Commit 3335cef

Browse files
committed
Added AV.Promise._debugError option to print callback unexpect error.
1 parent 4f421f8 commit 3335cef

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/promise.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
_.extend(AV.Promise, /** @lends AV.Promise */ {
4545

4646
_isPromisesAPlusCompliant: true,
47+
_debugError: false,
4748

4849
/**
4950
* Returns true iff the given object fulfils the Promise interface.
@@ -361,6 +362,9 @@
361362
try {
362363
result = [resolvedCallback.apply(this, result)];
363364
} catch (e) {
365+
if(AV.Promise._debugError && e) {
366+
console.error('Error occurred in promise resolve callback', e);
367+
}
364368
result = [AV.Promise.error(e)];
365369
}
366370
} else {
@@ -385,6 +389,9 @@
385389
try {
386390
result = [rejectedCallback(error)];
387391
} catch (e) {
392+
if(AV.Promise._debugError && e) {
393+
console.error('Error occurred in promise reject callback', e);
394+
}
388395
result = [AV.Promise.error(e)];
389396
}
390397
} else {
@@ -412,8 +419,14 @@
412419
func.call();
413420
};
414421
if (AV.Promise._isPromisesAPlusCompliant) {
415-
if (typeof(setImmediate) !== 'undefined' && _.isFunction(setImmediate)) {
416-
runLater = setImmediate;
422+
if (typeof(window) !== 'undefined' && _.isFunction(window.setImmediate)) {
423+
runLater = function(func) {
424+
window.setImmediate(func);
425+
};
426+
} else if (typeof(process) !== 'undefined' && process.nextTick) {
427+
runLater = function(func) {
428+
process.nextTick(func);
429+
};
417430
} else if (typeof(setTimeout) !== 'undefined' && _.isFunction(setTimeout)) {
418431
runLater = function(func) {
419432
setTimeout(func, 0);
@@ -428,7 +441,7 @@
428441
});
429442
} else if (this._rejected) {
430443
runLater(function() {
431-
wrappedRejectedCallback(self._error);
444+
wrappedRejectedCallback.apply(self, [self._error]);
432445
});
433446
} else {
434447
this._resolvedCallbacks.push(wrappedResolvedCallback);

test/promise.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ describe('promise', function() {
2323
});
2424
});
2525

26+
27+
describe('AV.Promise._debugError', function() {
28+
it('should log error', function(done) {
29+
AV.Promise._debugError = true;
30+
var p = new AV.Promise();
31+
p.then(function() {
32+
noThisMethod();
33+
});
34+
p.resolve(100);
35+
p.finally(function(){
36+
AV.Promise._debugError = false;
37+
done();
38+
});
39+
});
40+
});
41+
2642
describe('always and finally', function(){
2743
it('should call always and finally', function(done){
2844
var p = new AV.Promise(function(resolve, reject) {

0 commit comments

Comments
 (0)