Created
January 16, 2018 08:38
-
-
Save kucukkanat/343db99345664900f4395b7b57570d90 to your computer and use it in GitHub Desktop.
Manual implementation of Promise
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
function Promise(fn) { | |
var callback = null; | |
this.then = function(cb) { | |
callback = cb; | |
}; | |
function resolve(value) { | |
// force callback to be called in the next | |
// iteration of the event loop, giving | |
// callback a chance to be set by then() | |
setTimeout(function() { | |
callback(value); | |
}, 1); | |
} | |
fn(resolve); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment