Skip to content

Commit 5b8a7bd

Browse files
committed
Merge pull request #111 from killme2008/feature/2015.07.09
Feature/2015.07.09
2 parents 53bf1c6 + f631f0c commit 5b8a7bd

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/promise.js

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

4646
_isPromisesAPlusCompliant: true,
47+
_debugError: false,
48+
49+
setPromisesAPlusCompliant: function(isCompliant) {
50+
AV.Promise._isPromisesAPlusCompliant = isCompliant;
51+
},
52+
53+
setDebugError: function(enable) {
54+
AV.Promise._debugError = enable;
55+
},
4756

4857
/**
4958
* Returns true iff the given object fulfils the Promise interface.
@@ -361,6 +370,9 @@
361370
try {
362371
result = [resolvedCallback.apply(this, result)];
363372
} catch (e) {
373+
if(AV.Promise._debugError && e) {
374+
console.error('Error occurred in promise resolve callback.', e.stack || e);
375+
}
364376
result = [AV.Promise.error(e)];
365377
}
366378
} else {
@@ -385,6 +397,9 @@
385397
try {
386398
result = [rejectedCallback(error)];
387399
} catch (e) {
400+
if(AV.Promise._debugError && e) {
401+
console.error('Error occurred in promise reject callback.', e.stack || e);
402+
}
388403
result = [AV.Promise.error(e)];
389404
}
390405
} else {
@@ -412,8 +427,14 @@
412427
func.call();
413428
};
414429
if (AV.Promise._isPromisesAPlusCompliant) {
415-
if (typeof(setImmediate) !== 'undefined' && _.isFunction(setImmediate)) {
416-
runLater = setImmediate;
430+
if (typeof(window) !== 'undefined' && _.isFunction(window.setImmediate)) {
431+
runLater = function(func) {
432+
window.setImmediate(func);
433+
};
434+
} else if (typeof(process) !== 'undefined' && process.nextTick) {
435+
runLater = function(func) {
436+
process.nextTick(func);
437+
};
417438
} else if (typeof(setTimeout) !== 'undefined' && _.isFunction(setTimeout)) {
418439
runLater = function(func) {
419440
setTimeout(func, 0);
@@ -428,7 +449,7 @@
428449
});
429450
} else if (this._rejected) {
430451
runLater(function() {
431-
wrappedRejectedCallback(self._error);
452+
wrappedRejectedCallback.apply(self, [self._error]);
432453
});
433454
} else {
434455
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.setDebugError(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.setDebugError(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)