Last active
October 23, 2017 20:52
-
-
Save j03m/fda9a9e89d86ddc4217a4dbeb2d4e433 to your computer and use it in GitHub Desktop.
async init
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 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! | |
// } |
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 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; | |
} |
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 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