1

I created this http service:

(function () {
    "use strict";

    angular.module("inspectionReview").factory("inspectionReviewServices", ["$http", "config", inspectionReviewServices]);

    function inspectionReviewServices($http, config) {
        var serviceAddress = config.baseUrl + "api/InspectionReview/";
        var service = {
            getValues: getValues
        };
        return service;

        function getValues(objectId, frequencyId) {
            return $http.get(serviceAddress + "getValuesByObjectTypeId/" + objectId + "/" + frequencyId);
        }    
    }
})();

And here I use it in controller to get data:

function checkInspection() {
    var frequencyId = 5;
    inspectionReviewServices.getValues($scope.object.Id, frequencyId).then(function (result) {
       $scope.someData = result.data;
    });
}

When I call function checkInspection I need to wait until $scope.someData property is populated by data and only after it populated by data only then further rows can be executed.Currently I get promise and code executed further.

EDIT(according to the Darren's answer):

I changed my service and function that calls service:

function getValues2(objectId, frequencyId) {
    return $http.get(serviceAddress + "getValuesByObjectTypeId/" + objectId + "/" + frequencyId).then(function (result) {
        return result.data;
    });
}

and here is function that calls for service in controller:

    function checkInspectionReviewValidety() {
        var frequencyId = 5;
        return inspectionReviewServices.getValues2($scope.object.Id, frequencyId)
        }

But still I dont get desired result.

How can I change checkInspection function to make it wait until $scope.someData property is populated by data?

2
  • Well, you can't. Put logic that needs result.data in then block. Commented Aug 14, 2016 at 17:45
  • You have to pass in the work you want to do as a function. Commented Aug 14, 2016 at 19:55

2 Answers 2

1

You can use .then on the $http service method

$http.get(serviceAddress + "getValuesByObjectTypeId/" + objectId + "/" + frequencyId).then(function() {
  // Populate items from callback 
});
Sign up to request clarification or add additional context in comments.

3 Comments

All that will do is move where the promise is resolved - it doesn't make it synchronous so the OP will be left with the same issue.
I made some edit to the question.Can you see it now please?
@DarrenDavies, as Lex pointed out, it makes absolutely no difference.
1

You can't do that. The whole aspect of promises is to handle the async request and execute when the promise is resolved/rejected. If you want some piece of code to execute after data is filled up please move that part of the code into .then block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.