Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.