Skip to content

Instantly share code, notes, and snippets.

@j03m
Last active October 23, 2017 20:52
Show Gist options
  • Save j03m/fda9a9e89d86ddc4217a4dbeb2d4e433 to your computer and use it in GitHub Desktop.
Save j03m/fda9a9e89d86ddc4217a4dbeb2d4e433 to your computer and use it in GitHub Desktop.
async init
function Model(stuff) {
this.d_stuffInterface = stuff;
}
Model.prototype.init = function (){
return this.d_stuffInterface.getData().then(function(data){
this.d_data = data;
return data;
}.bind(this);
}
//Don't provide this, force all work in init
// Model.prototype.getData = function(){
// //need to assert continueInit was called!
// }
function Model(stuff) {
this.d_stuffInterface = stuff;
this._initDefer = createDefer();
}
Model.prototype.init = function(){
this.d_stuffInterface.getData().then(function(data){
this._initDefer.resolve(data);
}.bind(this);
}
//getData is a cached promise all access happens that way
//this should probably be a getter
Model.prototype.getData = function(){
return this._initDefer.promise;
}
function createDefer(){
var deferal = {};
deferal.promise = new Promise(function(resolve,reject){
deferal.resolve = resolve;
deferal.reject = reject;
});
return deferal;
}
function Model(stuff) {
this.d_stuffInterface = stuff;
}
//getting data is init
Model.prototype.getData = function(){
if (!this.initP){
this.initP = this.d_stuffInterface.getData();
}
return this.initP.then(function(data){
return data;
}.bind(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment