Imagine this object I am constructing.
$scope.formules = {};
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
$scope.getData().then(function(){
// Some code to fetch the correct data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
}();
}
Currently, getYear is undefined because I need to include a second return.
$scope.formules = {};
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
$scope.getData().then(function(){
// Some code to fetch the correct data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
return $scope.getData(); //Adding the second return here
}();
Alternatively, you could do it in a shorter way like this:
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
return $scope.getData().then(function(){ // Or adding the second return here.
// Some code to produce the right data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
}();
However, this approach isn't ideal as the second return only returns the promise, making my method getYear
contain a promise instead of the desired value.
One more attempt:
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
return $scope.getData().then(function(){ // return the $scope.getData()
// Some code to produce the right data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
}().then(function(data){ return data; });
Even after trying multiple ways, console.log($scope.formules)
still shows that getYear is a promise and doesn't provide the actual value.
Note: When using console.log(data)
instead of return data;
, I am able to retrieve the desired value for binding to the method.
What am I missing here? What is the correct way to bind the value? Neither Stack Overflow nor Google has provided me with an answer...
EDIT
Here is the exact code segment used inside the method:
getYear: function () {
$scope.measure = value.formules_total_year;
$scope.getCube().then(function(test){
var totalRatioData = [];
for (var i = 0; i < $scope.tempData.length; i++){
totalRatioData.push($scope.tempData[i][1].qNum);
}
return totalRatioData;
})
}(),
$scope.tempData is an array containing 5 arrays.
[Array[2], Array[2], Array[2], Array[2], Array[2]]
with each array consisting of 2 objects.
0: Object
qElemNumber: 0
qNum: 2009
qState: "O"
qText: "2009"
__proto__: Object
1: Object
qElemNumber: 0
qNum: 225632.21000000002
qState: "L"
qText: "225632,21"
In this edited version, I aim to create a new array by extracting all the qNum values from the last object of the 5 arrays and assign this array to getYear.