|
44 | 44 | _.extend(AV.Promise, /** @lends AV.Promise */ { |
45 | 45 |
|
46 | 46 | _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 | + }, |
47 | 56 |
|
48 | 57 | /** |
49 | 58 | * Returns true iff the given object fulfils the Promise interface. |
|
361 | 370 | try { |
362 | 371 | result = [resolvedCallback.apply(this, result)]; |
363 | 372 | } catch (e) { |
| 373 | + if(AV.Promise._debugError && e) { |
| 374 | + console.error('Error occurred in promise resolve callback.', e.stack || e); |
| 375 | + } |
364 | 376 | result = [AV.Promise.error(e)]; |
365 | 377 | } |
366 | 378 | } else { |
|
385 | 397 | try { |
386 | 398 | result = [rejectedCallback(error)]; |
387 | 399 | } catch (e) { |
| 400 | + if(AV.Promise._debugError && e) { |
| 401 | + console.error('Error occurred in promise reject callback.', e.stack || e); |
| 402 | + } |
388 | 403 | result = [AV.Promise.error(e)]; |
389 | 404 | } |
390 | 405 | } else { |
|
412 | 427 | func.call(); |
413 | 428 | }; |
414 | 429 | 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 | + }; |
417 | 438 | } else if (typeof(setTimeout) !== 'undefined' && _.isFunction(setTimeout)) { |
418 | 439 | runLater = function(func) { |
419 | 440 | setTimeout(func, 0); |
|
428 | 449 | }); |
429 | 450 | } else if (this._rejected) { |
430 | 451 | runLater(function() { |
431 | | - wrappedRejectedCallback(self._error); |
| 452 | + wrappedRejectedCallback.apply(self, [self._error]); |
432 | 453 | }); |
433 | 454 | } else { |
434 | 455 | this._resolvedCallbacks.push(wrappedResolvedCallback); |
|
0 commit comments