how can I run a function "searchBoats" with two parameters when these two parameters are returned as results from previous functions in AngularJS when these two functions are calling promise methods?
var parseURL = function() {
var language = $routeParams.language;
var url = $location.path();
boatType = parseBoatType(url, language);
var destination = parseDestination(url, language);
searchBoats(destination, boatType); };
parseURL();
var parseBoatType = function(url, language) {
var boatType = UrlService.parseUrlBoatType(url, language);
var boatTypeParsed = UrlService.parseBoatType(url, language);
// boat type is parsed
if(boatTypeParsed) {
BoatType.getBoatTypeByName({name: boatTypeParsed}, function success(result) {
boatTypeAux = result;
return boatTypeAux;
});
}
else {
return null;
}
};
var parseDestination = function(url, language) {
// departure is parsed
var departure = UrlService.parseUrlDeparture(url);
return $http.get("http://" + API_SERVER_URL + "/translatedDepartures?departure="+departure+";lang="+ language+";matchStart="+true).then(function(response) {
departure = response.data.map(function(source) {
return source.element_translation;
});
...
Note: When I run BoatType.getBoatTypeByName in parseBoatType function the code is still running and I run searchBoats before I get the results.
Update:
searchBoats methods will look like:
var searchBoats = function(destination, boatType) {
Boat.query({
destination: destination,
boatType: boatType
}, function success(result) {
console.log("getOptionList.query realizada");
$scope.boats = result;
...
According to your answer I have a callback BoatType.getBoatTypeByName({name: boatTypeParsed}, function success(result) { boatTypeAux = result;
calling to a factory service to my api:
angular.factory('BoatType',
function ($resource, SERVER_URL) {
var boatTypes =
$resource('http://' + SERVER_URL + '/:action', {
action: 'boat_types'
}, {
query: {
method: 'GET',
isArray: true
},
getBoatTypeById: {
method: 'GET',
params: {
action: 'getBoatTypeById'
},
isArray: false
},
getBoatTypesById: {
method: 'GET',
params: {
action: 'getBoatTypesById'
},
isArray: true
},
getBoatTypeByName: {
method: 'GET',
params: {
action: 'getBoatTypeByName'
},
isArray: false
}
});
return boatTypes;
}
)
New update According to the comments of @zero298, now my code looks like this:
var parseURL = function() {
var language = $routeParams.language;
var url = $location.path();
// You need to wait on your async calls
var boatsPromise = $q.all([
parseBoatType(url, language),
parseDestination(url, language)
]).then(function(resultArr){
var boatType = resultArr[0],
destination = resultArr[1];
// Return something else to wait on
return searchBoats(destination, boatType);
});
var parseBoatType = function(url, language) {
BoatType.getBoatTypeByName({name: boatTypeParsed}, function success(result) {
return result;
});
};
var parseDestination = function(url, language) {
return "whatever";
};
// The service
.factory('BoatType',
function($resource, SERVER_URL){
var boatTypes =
$resource('http://' + SERVER_URL +'/:action', {action:'boat_types'}, {
query: {method:'GET', isArray: true},
getBoatTypeByName: {method:'GET', params:{action: 'getBoatTypeByName'}, isArray: false}
});
return boatTypes;
}
)
Although, the function searchBoats waits until parseBoatType and parseDestination are executed, note parseBoatType has a callback to a service with a $resource call to an API (which I think is asynchnous), so as result, the searchBoats function is executed before the callback gets the result.