-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unhanded rejection warnings. Updated version to 4.0.1
- Loading branch information
1 parent
2f8e889
commit c381e1f
Showing
6 changed files
with
391 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
(function (root) { | ||
|
||
// Store setTimeout reference so promise-polyfill will be unaffected by | ||
// other code modifying setTimeout (like sinon.useFakeTimers()) | ||
var setTimeoutFunc = setTimeout; | ||
|
||
function noop() { | ||
} | ||
|
||
// Use polyfill for setImmediate for performance gains | ||
var asap = (typeof setImmediate === 'function' && setImmediate) || | ||
function (fn) { | ||
setTimeoutFunc(fn, 1); | ||
}; | ||
|
||
var onUnhandledRejection = function onUnhandledRejection(err) { | ||
console.warn('Possible Unhandled Promise Rejection:', err); // eslint-disable-line no-console | ||
}; | ||
|
||
// Polyfill for Function.prototype.bind | ||
function bind(fn, thisArg) { | ||
return function () { | ||
fn.apply(thisArg, arguments); | ||
}; | ||
} | ||
|
||
var isArray = Array.isArray || function (value) { | ||
return Object.prototype.toString.call(value) === '[object Array]'; | ||
}; | ||
|
||
function Promise(fn) { | ||
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new'); | ||
if (typeof fn !== 'function') throw new TypeError('not a function'); | ||
this._state = 0; | ||
this._handled = false; | ||
this._value = undefined; | ||
this._deferreds = []; | ||
|
||
doResolve(fn, this); | ||
} | ||
|
||
function handle(self, deferred) { | ||
while (self._state === 3) { | ||
self = self._value; | ||
} | ||
if (self._state === 0) { | ||
self._deferreds.push(deferred); | ||
return; | ||
} | ||
self._handled = true; | ||
asap(function () { | ||
var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected; | ||
if (cb === null) { | ||
(self._state === 1 ? resolve : reject)(deferred.promise, self._value); | ||
return; | ||
} | ||
var ret; | ||
try { | ||
ret = cb(self._value); | ||
} catch (e) { | ||
reject(deferred.promise, e); | ||
return; | ||
} | ||
resolve(deferred.promise, ret); | ||
}); | ||
} | ||
|
||
function resolve(self, newValue) { | ||
try { | ||
// Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure | ||
if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.'); | ||
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) { | ||
var then = newValue.then; | ||
if (newValue instanceof Promise) { | ||
self._state = 3; | ||
self._value = newValue; | ||
finale(self); | ||
return; | ||
} else if (typeof then === 'function') { | ||
doResolve(bind(then, newValue), self); | ||
return; | ||
} | ||
} | ||
self._state = 1; | ||
self._value = newValue; | ||
finale(self); | ||
} catch (e) { | ||
reject(self, e); | ||
} | ||
} | ||
|
||
function reject(self, newValue) { | ||
self._state = 2; | ||
self._value = newValue; | ||
finale(self); | ||
} | ||
|
||
function finale(self) { | ||
if (self._state === 2 && self._deferreds.length === 0) { | ||
setTimeout(function() { | ||
if (!self._handled) { | ||
onUnhandledRejection(self._value); | ||
} | ||
}, 1); | ||
} | ||
|
||
for (var i = 0, len = self._deferreds.length; i < len; i++) { | ||
handle(self, self._deferreds[i]); | ||
} | ||
self._deferreds = null; | ||
} | ||
|
||
function Handler(onFulfilled, onRejected, promise) { | ||
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null; | ||
this.onRejected = typeof onRejected === 'function' ? onRejected : null; | ||
this.promise = promise; | ||
} | ||
|
||
/** | ||
* Take a potentially misbehaving resolver function and make sure | ||
* onFulfilled and onRejected are only called once. | ||
* | ||
* Makes no guarantees about asynchrony. | ||
*/ | ||
function doResolve(fn, self) { | ||
var done = false; | ||
try { | ||
fn(function (value) { | ||
if (done) return; | ||
done = true; | ||
resolve(self, value); | ||
}, function (reason) { | ||
if (done) return; | ||
done = true; | ||
reject(self, reason); | ||
}); | ||
} catch (ex) { | ||
if (done) return; | ||
done = true; | ||
reject(self, ex); | ||
} | ||
} | ||
|
||
Promise.prototype['catch'] = function (onRejected) { | ||
return this.then(null, onRejected); | ||
}; | ||
|
||
Promise.prototype.then = function (onFulfilled, onRejected) { | ||
var prom = new Promise(noop); | ||
handle(this, new Handler(onFulfilled, onRejected, prom)); | ||
return prom; | ||
}; | ||
|
||
Promise.all = function () { | ||
var args = Array.prototype.slice.call(arguments.length === 1 && isArray(arguments[0]) ? arguments[0] : arguments); | ||
|
||
return new Promise(function (resolve, reject) { | ||
if (args.length === 0) return resolve([]); | ||
var remaining = args.length; | ||
|
||
function res(i, val) { | ||
try { | ||
if (val && (typeof val === 'object' || typeof val === 'function')) { | ||
var then = val.then; | ||
if (typeof then === 'function') { | ||
then.call(val, function (val) { | ||
res(i, val); | ||
}, reject); | ||
return; | ||
} | ||
} | ||
args[i] = val; | ||
if (--remaining === 0) { | ||
resolve(args); | ||
} | ||
} catch (ex) { | ||
reject(ex); | ||
} | ||
} | ||
|
||
for (var i = 0; i < args.length; i++) { | ||
res(i, args[i]); | ||
} | ||
}); | ||
}; | ||
|
||
Promise.resolve = function (value) { | ||
if (value && typeof value === 'object' && value.constructor === Promise) { | ||
return value; | ||
} | ||
|
||
return new Promise(function (resolve) { | ||
resolve(value); | ||
}); | ||
}; | ||
|
||
Promise.reject = function (value) { | ||
return new Promise(function (resolve, reject) { | ||
reject(value); | ||
}); | ||
}; | ||
|
||
Promise.race = function (values) { | ||
return new Promise(function (resolve, reject) { | ||
for (var i = 0, len = values.length; i < len; i++) { | ||
values[i].then(resolve, reject); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* Set the immediate function to execute callbacks | ||
* @param fn {function} Function to execute | ||
* @private | ||
*/ | ||
Promise._setImmediateFn = function _setImmediateFn(fn) { | ||
asap = fn; | ||
}; | ||
|
||
Promise._setUnhandledRejectionFn = function _setUnhandledRejectionFn(fn) { | ||
onUnhandledRejection = fn; | ||
}; | ||
|
||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = Promise; | ||
} else if (!root.Promise) { | ||
root.Promise = Promise; | ||
} | ||
|
||
})(this); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ module.exports = { | |
obj.promise = prom; | ||
return obj; | ||
} | ||
} | ||
}; |
Oops, something went wrong.