Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active January 14, 2017 03:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ORESoftware/7ba04b0f1725000609aa7335c3eb9984 to your computer and use it in GitHub Desktop.
Save ORESoftware/7ba04b0f1725000609aa7335c3eb9984 to your computer and use it in GitHub Desktop.
An asynchronous constructor pattern
// asynchronous constructor example,
// like always do not explictly return anything from the constructor
function Queue(){
this.ready = false;
let callable = true;
let ee = new EE(); // new event emitter
// the init method represents our real constructor
this.init = function (){
if(this.ready){
return simpleSyncObservable();
}
if(!callable){
// we already called the init method, but aren't yet ready
return simpleEventObservable(ee);
}
callable = false;
// we only want to invoke the below chain once
// whichever method calls it first
return someObservable()
.do(() => {
this.ready = true;
ee.emit('ready');
});
}
}
// every method will probably need to call init() first before anything
Queue.prototype.obs7 = function(){
// most of our methods start with init()
return this.init()
.mapTo(() => 7);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment