I have a service. Lets say that this returns the string "B". I have the following controller:
$scope.output;
var someVariable = 'A';
$scope.setUpConnection = function (someVariable) {
$scope.output = createConnection(someVariable);
console.log($scope.output);
}
createConnection = function (someVariable) {
var result;
service.connect(someVariable).then(function success(response) {
result = response.data;
}, function error(response) {
result = 'Error' + response;
});
return result;
}
The createConnection() function jumps to the end of the function, not waiting for the .then, returning nothing and as such returning undefined. Can someone explain why it isn't waiting for the promise to finish?
The service isn't the problem, if I replace the return with a direct
$scope.output = response.data;
it works fine. The problem and the reason it's set up like this is that the createConnection() function will eventually take place in a seperate JS file once I get around to moving it, so the solution above won't work. As such I figured I'd need the return statement.