I am looking for a way to achieve the following:
var promise = IAmAEmptyPromise;
if(condition){
promise = ApiService.getRealPromise();
}
promise.then(function(){
//perform some actions
});
I want to define a promise that can be resolved with a then function. However, this promise might get replaced by another promise that returns some data. After that, I would like to resolve the promise regardless of whether it has any data or not. Is this feasible? My attempt was:
var promise = $q.defer().promise;
if(!$scope.user){
promise = UserService.create(params);
}
promise.then(function(){
//handle the scenario where a new user is created or an existing user is found
});
The above code does not work as expected when a user already exists. Any suggestions on how to make it work in all cases?