Skip to content

Instantly share code, notes, and snippets.

@gerardpaapu
Created December 16, 2012 08:05
Show Gist options
  • Save gerardpaapu/4304161 to your computer and use it in GitHub Desktop.
Save gerardpaapu/4304161 to your computer and use it in GitHub Desktop.
Having a play with typescript, seems likeable enough so far.
var Promise = (function () {
function Promise() {
this.isResolved = false;
this.didSucceed = false;
this.value = null;
this.error = null;
this.resolveListeners = [];
this.progressHandlers = [];
}
Promise.prototype.then = function (successHandler, errorHandler, progressHandler) {
var result = new Promise();
var listener = function (didSucceed, value, error) {
var next = null;
if(didSucceed) {
next = successHandler ? successHandler(value) : null;
} else {
next = errorHandler ? errorHandler(error) : null;
}
if(next != null && (typeof next.then) === 'function') {
next.then(function (value) {
return result.succeedWith(value);
}, function (error) {
return result.failWith(value);
}, function (message) {
return result.progressWith(message);
});
} else {
if(didSucceed) {
result.succeedWith(value);
} else {
result.failWith(error);
}
}
};
if(this.isResolved) {
listener.call(this, this.didSucceed, this.value, this.error);
} else {
this.resolveListeners.push(listener);
}
this.progressHandlers.push(progressHandler);
return result;
};
Promise.prototype.resolve = function (didSucceed, value, error) {
var _this = this;
if(this.isResolved) {
return;
}
this.isResolved = true;
this.didSucceed = didSucceed;
if(this.didSucceed) {
this.value = value;
} else {
this.error = error;
}
this.resolveListeners.forEach(function (f) {
return f.call(_this, didSucceed, value, error);
});
this.resolveListeners = null;
};
Promise.prototype.succeedWith = function (value) {
this.resolve(true, value, null);
};
Promise.prototype.failWith = function (error) {
this.resolve(false, null, error);
};
Promise.prototype.progressWith = function (message) {
var _this = this;
this.progressHandlers.forEach(function (handler) {
return handler.call(_this, message);
});
};
Promise.succeedWith = function succeedWith(value) {
var promise = new Promise();
promise.succeedWith(value);
return promise;
}
Promise.failWith = function failWith(error) {
var promise = new Promise();
promise.failWith(error);
return promise;
}
return Promise;
})();
class Promise {
public then(
successHandler: (value) => Promise,
errorHandler: (error) => Promise,
progressHandler: (value) => void) : Promise
{
var result = new Promise();
var listener = function (didSucceed, value, error) {
var next : Promise = null;
if (didSucceed) {
next = successHandler ? successHandler(value) : null;
} else {
next = errorHandler ? errorHandler(error) : null
}
if (next != null &&
(typeof next.then) === 'function') {
next.then(
(value) => result.succeedWith(value),
(error) => result.failWith(value),
(message) => result.progressWith(message));
} else if (didSucceed) {
result.succeedWith(value);
} else {
result.failWith(error)
}
};
if (this.isResolved) {
listener.call(this, this.didSucceed, this.value, this.error);
} else {
this.resolveListeners.push(listener);
}
this.progressHandlers.push(progressHandler);
return result;
}
private isResolved : bool = false;
private didSucceed : bool = false;
private value : any = null;
private error : any = null;
private resolveListeners : any[] = [];
private progressHandlers : any[] = [];
private resolve(didSucceed : bool, value, error) {
if (this.isResolved) return;
this.isResolved = true;
this.didSucceed = didSucceed;
if (this.didSucceed) {
this.value = value;
} else {
this.error = error;
}
this.resolveListeners.forEach(
(f) => f.call(this, didSucceed, value, error));
this.resolveListeners = null;
}
public succeedWith(value) {
this.resolve(true, value, null);
}
public failWith(error) {
this.resolve(false, null, error);
}
public progressWith(message) {
this.progressHandlers.forEach(
(handler) => handler.call(this, message));
}
public static succeedWith(value) {
var promise = new Promise();
promise.succeedWith(value);
return promise;
}
public static failWith(error) {
var promise = new Promise();
promise.failWith(error);
return promise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment