Last active
March 26, 2016 11:54
-
-
Save icefapper/de509f247a9b3df5bac6 to your computer and use it in GitHub Desktop.
small but functioning impl of a promise
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 w(){this.state=null;this.c={};this.result={value:null,pending:false};} | |
w.prototype.toState=function(state,result){this.state=state;if(this.c[state]){var l=this;setTimeout(function(){l.c[state](result)}, 0);}else{this.result.value=result;this.result.pending=12;}} | |
w.prototype.onState = function(state,c){this.state=state;if(this.result.pending)this.toState(state,this.result.value ) ; } | |
function w5sec() { | |
var l = new w(); | |
setTimeout(function(){l.toState("resolve",12);} , 5000) | |
return l | |
} | |
w5sec().onState("resolve",function(value){console.log(value, "resolved" ) } ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
small impl for a promise