**In my service:**

```
appRoot.factory('ProgramsResource', function ($resource) {
    return $resource('Home/Program', {}, { Program: { method: 'get', isArray: false } })
});

```

**In my controller:**

```
appRoot.controller('ProgramCtrl', function ($scope, ProgramsResource) {
    $scope.searchPrograms = function () {
        $scope.Programs = ProgramsResource.get(
            {
                Name: $scope.searchProgramName,
            });
    };

    $scope.SortBy = "Name";
    $scope.searchPrograms();

    //Lines of code which I want to execute only after the searchPrograms() completes its execution
    $scope.TotalItems = $scope.Programs.TotalItems;
    $scope.ItemsPerPage = $scope.Programs.ItemsPerPage;
});

```

**searchPrograms();** is responsible for getting data from server. And only after line **$scope.searchPrograms();** I want to execute below code:

```
$scope.TotalItems = $scope.Programs.TotalItems;
$scope.ItemsPerPage = $scope.Programs.ItemsPerPage;

```

But its not happening like that. Its not waiting for searchPrograms() to complete its operation and executing the lines of code below it.
As in js, it won't wait for ajax to complete and executes the lines below it, this is happening.
To execute certain code only after ajax completion there is concept of call back functions in js and for the same there is concept of promises in angular.

I got very nice [article](http://andyshora.com/promises-angularjs-explained-as-cartoon.html) over angular promises but not able to understand that, how exactly I should use the promises in my case.