1
function MakePromise (asap) {
3
if (typeof this !== 'object' || typeof fn !== 'function') throw new TypeError();
8
doResolve(fn, resolve.bind(this), reject.bind(this));
11
function handle(deferred) {
13
if (this._state === null) {
14
this._deferreds.push(deferred);
18
var cb = me._state ? deferred.onFulfilled : deferred.onRejected
19
if (typeof cb !== 'function') {
20
(me._state ? deferred.resolve : deferred.reject)(me._value);
31
deferred.resolve(ret);
35
function resolve(newValue) {
36
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
37
if (newValue === this) throw new TypeError();
38
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
39
var then = newValue.then;
40
if (typeof then === 'function') {
41
doResolve(then.bind(newValue), resolve.bind(this), reject.bind(this));
46
this._value = newValue;
48
} catch (e) { reject.call(this, e); }
51
function reject(newValue) {
53
this._value = newValue;
58
for (var i = 0, len = this._deferreds.length; i < len; i++) {
59
handle.call(this, this._deferreds[i]);
61
this._deferreds = null;
65
* Take a potentially misbehaving resolver function and make sure
66
* onFulfilled and onRejected are only called once.
68
* Makes no guarantees about asynchrony.
70
function doResolve(fn, onFulfilled, onRejected) {
77
}, function (reason) {
89
Promise.prototype['catch'] = function (onRejected) {
90
return this.then(null, onRejected);
93
Promise.prototype.then = function(onFulfilled, onRejected) {
95
return new Promise(function(resolve, reject) {
97
onFulfilled: onFulfilled,
98
onRejected: onRejected,
105
Promise.resolve = function (value) {
106
if (value && typeof value === 'object' && value.constructor === Promise) {
110
return new Promise(function (resolve) {
115
Promise.reject = function (value) {
116
return new Promise(function (resolve, reject) {
125
if (typeof module !== 'undefined') {
126
module.exports = MakePromise;