Max Koretskyi
Max Koretskyi

Reputation: 105497

return resolved promise and execute the code after resolve

I have the following code which returns a promise, but also should execute some code:

if (fileExists()) {
     return $q.when().then(function () {
         $scope.$broadcast('create-file-success', file);
     });
}

I sense that this is not the best way to do that, maybe I should use some thing like that:

 return $q.when(function () {
     $scope.$broadcast('create-file-success', file);
 });

or like that:

 return $q(function () {
     $scope.$broadcast('create-file-success', file);
 });

But I can't figure out from the documentation whether the last two do the same.

Upvotes: 1

Views: 435

Answers (1)

Vaibhav Pachauri
Vaibhav Pachauri

Reputation: 2671

It should be something like following :

 $q.when(fileExists()).then(function (data) {
     console.log('Resolved with the value data', data);
     //do whatever you want to do with the resolved object and data.
     $scope.$broadcast('create-file-success', file);
 });

I hope this helps :)

Upvotes: 1

Related Questions