0

I have a controller making two $resource calls against two REST services where the result of the first one is used as input by the second one.

Here the code:

if (requestLock == false) {
    $scope.T_01_04_sharedData.tempRequestForT_01_04 = insertNewRequest("aggr_1", $rootScope.globals.currentUser.username, "", "temp", "2016-07-30 00:00:00");
    requestLock = true;
}

if (action == 'add') {
    updateSelectedForRequest(prosumer, 'selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
} else {
    updateSelectedForRequest(prosumer, 'non-selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
}

Function updateSelectedForRequest

function updateSelectedForRequest(username, status, businessUser, request) {
            WidgetService.T_01_04_updateSelectedForRequest.query({
                businessUser_id: businessUser,
                request_id: request,
                username: username,
                status: status
            }, function (result) {
                // response handler

            });
        }

Function insertNewRequest

function insertNewRequest(bu_id_target, requester, description, status, validUntil) {
            return WidgetService.T_01_04_insertNewRequest.query({
                bu_id_target: bu_id_target, 
                requester: requester,
                description: description,
                status: status,
                validUntil: validUntil

            }, function (result) {

                $scope.T_01_04_sharedData.tempRequestForT_01_04 = result.request_id;
                return result;
            });
        }

The error is that the first call is not resolved sequentially so the second one has no input.

Is there the possibility to run these two calls sequentially in order to wait for the second call the input from the first one?

Thanks a lot.

Pietro Fragnito
  • 327
  • 1
  • 4
  • 18

1 Answers1

0

I'm not familiar with ngresource, but you can try something like this.

if (requestLock == false) {
    insertNewRequest("aggr_1", $rootScope.globals.currentUser.username, "", "temp", "2016-07-30 00:00:00")
    .then(function(result){
         $scope.T_01_04_sharedData.tempRequestForT_01_04 = result;

         if (action == 'add') {
              updateSelectedForRequest(prosumer, 'selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
         } else {
              updateSelectedForRequest(prosumer, 'non-selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
         }
    }, function(error){/* manage error here */});

    requestLock = true;
}

function insertNewRequest(bu_id_target, requester, description, status, validUntil) {
            return new Promise(function(resolve, reject){
                PromiseWidgetService.T_01_04_insertNewRequest.query({
                    bu_id_target: bu_id_target, 
                    requester: requester,
                    description: description,
                    status: status,
                    validUntil: validUntil

                }, function (result) {

                    $scope.T_01_04_sharedData.tempRequestForT_01_04 = result.request_id;
                    resolve(result);
                });
            })
        }

more information on promise here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Yoann
  • 446
  • 3
  • 6