0

I understand that the traditional way to wait for remote data is to .then from promises but the Problem is I need to create a function that returns the value created by the .then

//inside controller
$scope.someFunction = function(id){
   var returnVal;
   $http.post(someURL,{_id:id}).then(
       function(r){
           //lets assume r.data = {id:123,name:'john doe'}
           returnVal = r.data.name;
       },function(r){
           console.error(r.statusText);
           returnVal = 'error getting name'
       }    
   );
   return returnVal;
};

The problem is it keeps returning a null result because it executes the return block even when .then was not called yet.

//the html is like this
<main component>
    <!--this main component will generate dynamic number of subs-->
    <!--lets assume Seller is the main and sub is all the products he sold -->
    <sub component sub-detail="the function that returns correct value">
        <!--I need to make $http request to get the product details for each product generated-->
    </sub component>
</main component>
<another main component>
    <!--generates another series of subs-->
</another main component>

so in short. I need a function() that returns data from an http request

anaval
  • 1,130
  • 10
  • 24
  • What's the point of calling a function (`http.post`) that runs _async_ if you want the outer function to be synchronous? – Maria Ines Parnisari Jul 04 '16 at 02:27
  • I am using a telerik component that generates another sub-component. It allows me to specify the attributes before it generates the sub-component but I need to get the data from a remote service. – anaval Jul 04 '16 at 02:44
  • 1
    If this is in controller why are you needing to return anything and not just assign scope model properties within the callback? – charlietfl Jul 04 '16 at 02:50
  • because the generated sub-component is not visible while coding the html. It needs to have dynamic data. – anaval Jul 04 '16 at 02:54
  • So does everything in angular, what makes yours different? Provide [mcve] – charlietfl Jul 04 '16 at 02:55
  • Perhaps you should look to using a routing resolve...or just pass the promise to your other component – charlietfl Jul 04 '16 at 02:56
  • I would love to do that, but what if it generates thousands of sub components? – anaval Jul 04 '16 at 03:02
  • can you remove the duplicate mark @charlie? – anaval Jul 04 '16 at 03:07
  • Show the js related to component and explain what you need from the controller first. This is turning into a completely different XY Problem question. Also a routing `resolve` would do exactly what you want to have data there before view renders – charlietfl Jul 04 '16 at 03:10
  • I can't, I signed a non-disclosure agreement(this project is from my internship btw), I edited my question to show a pseudo-code of the actual html – anaval Jul 04 '16 at 03:13
  • Well without [mcve] is hard to help solve whatever problem is – charlietfl Jul 04 '16 at 03:38

1 Answers1

-2

Update your code:

$scope.someFunction = function(id){
   var returnVal;
   return $http.post(someURL,{_id:id}).then(
       function(r){
           //lets assume r.data = {id:123,name:'john doe'}
           returnVal = r.data.name;
           return returnVal;
       },function(r){
           console.error(r.statusText);
           returnVal = 'error getting name';
           return returnVal;
       }    
   );
};
mtamma
  • 533
  • 4
  • 6