Skip to content

Instantly share code, notes, and snippets.

@bidiu
Created March 12, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bidiu/e75a7c54d7428b7f2ee2592b3ae3a7ad to your computer and use it in GitHub Desktop.
Save bidiu/e75a7c54d7428b7f2ee2592b3ae3a7ad to your computer and use it in GitHub Desktop.
function Promise(func) {
var state = 'pending';
var deferred = null; // the "then" handler
var value;
function resolve(newValue) {
state = 'resolved';
value = newValue;
if (deferred) {
handle(deferred);
}
}
function reject(reason) {
state = 'rejected';
value = reason;
if (deferred) {
handle(deferred);
}
}
function handle(handler) {
if (state === 'pending') {
deferred = handler;
return;
}
}
this.then = function () {
}
func(resolve, reject);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment