Assume I need to fetch a value only if an object is already present in my model. Otherwise, I should retrieve the output of an endpoint service:
model.getDoohkyById = function( id ){
if( this.data ) {
if( this.data.length > 0) {
for( var i =0; i < this.data.length; i++){
if( this.data[i].id === id ){
//this provides a return value
return this.data[i];
}
}
}
}
// this provides a promise
return this.service.getBy('id',id);
}
In what way can I structure the initial return value within the scope of a promise? This way, I can execute it without encountering the error object has no method 'then'
?
DoohkyModel.getDoohkyById(this.doohkyId).then( function(result){
that.doohky = result.data;
});