Created
March 12, 2019 19:55
-
-
Save bidiu/e75a7c54d7428b7f2ee2592b3ae3a7ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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