Skip to content

Instantly share code, notes, and snippets.

@icefapper
Last active March 26, 2016 11:54
Show Gist options
  • Save icefapper/de509f247a9b3df5bac6 to your computer and use it in GitHub Desktop.
Save icefapper/de509f247a9b3df5bac6 to your computer and use it in GitHub Desktop.
small but functioning impl of a promise
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" ) } )
@icefapper
Copy link
Author

small impl for a promise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment