-2

I am trying to use $q.all in the code below. But I think I've misunderstood some key ideas because it's not working as I expected. If anyone could give me some pointers that would be really appreciated.

The issue is in $q.all(toSend.pie.slices).then():

var someData = {...};
var toSend = {
    pie: {
        slices: []
    }
};

toSend.pie.slices = generatePieSlice(someData);


$q.all(toSend.pie.slices).then(function(data){
    if(data) {      
        console.log(data);  // this is undefined :(        
        //do something else
    }
});  



function generatePieSlice(data) {
    var arr = [];
    if(data) {
        angular.forEach(data, function(resp_o, resp_n){
            arr.push({
                name: resp_o.display,
                marketValue: resp_o.value,
                percentage: resp_o.percentage,
                key: resp_n
            });
        });
    }
    $q.all(arr).then(function(data) {
        if(data) {
            console.log(data); // this gives me with the correct data
            return data;
        }
    });

}
muudless
  • 7,422
  • 7
  • 39
  • 57
  • 2
    `$q.all` is for a collection of promises. I don't see any promises in either your `toSend.pie.slices` or `arr` arrays. Also, `generatePieSlice` doesn't return anything – Phil Jul 24 '15 at 04:56
  • @Phil, thanks for your tips, it makes sense to me theoretically, but I'm not too sure how to implement it. Can you please show me what the working code might be? – muudless Jul 24 '15 at 05:10
  • 1
    For some reason you are doing `$q.all()` twice. Omit the second call, `toSend.pie.slices` is already a promise (if you return it from `generatePieSlice`), you can invoke `.then` on it directly. – Bergi Jul 24 '15 at 05:18
  • @Bergi, but I thought it's better to not manipulate a global variable directly in a function? Thats why I'm trying to return the array through the generatePieSlice function. – muudless Jul 24 '15 at 05:21
  • 1
    @muudless: Which global variable do you mean? And yes, you should definitely `return` something from your `generatePieSlice` function. – Bergi Jul 24 '15 at 05:23
  • @Bergi, I meant `toSend.pie.slices`...I dont think I've understood you correctly - can you please give me a bit more info? – muudless Jul 24 '15 at 05:27
  • You're not manipulating it from within that function. You're just assigning to it the result of a function call. – Bergi Jul 24 '15 at 05:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84135/discussion-between-muudless-and-bergi). – muudless Jul 24 '15 at 05:35
  • *"Can you please show me what the working code might be?"* ~ No because I don't know what you're trying to do. I see no reason to use `$q` at all in your code. You have **no promises** – Phil Jul 24 '15 at 05:35
  • @Phil ok no problem, obviously I'm asking a question because I don't understand, don't have to get upset :) Thanks for your help, I will read up more about it. – muudless Jul 24 '15 at 05:43
  • you can also refer to my question to get idea: http://stackoverflow.com/questions/16316940/how-to-resolve-q-all-promises-in-jasmine-unit-tests – Rahul R. Jul 24 '15 at 06:23
  • @RahulR. thank you so much, thats given me a good direction for my next task! – muudless Jul 24 '15 at 07:52

1 Answers1

-3

Thanks for all the commenters' help. This is my eventual solution for anyone else interested :)

var someData = {...};
var toSend = {
    pie: {
        slices: []
    }
};


$q.all({

    pieSlices: generatePieSlice(someData)

}).then(function(data){

    if(data) {      
        toSend = {
            pie: {
                slices: data.pieSlices
            }
        };

        // deferred.resolve(toSend) or something else :)
    }

});  


function generatePieSlice(response) {
    var arr = [];
    if(response) {
        angular.forEach(data, function(resp_o, resp_n){
            arr.push({
                name: resp_o.display,
                marketValue: resp_o.value,
                percentage: resp_o.percentage,
                key: resp_n
            });
        });
    }                
    return arr;
}
muudless
  • 7,422
  • 7
  • 39
  • 57
  • yes, but this doesn't make sense as long as you don't have any *async* operations and this is an overkill. – code-jaff Jul 24 '15 at 07:01
  • @code-jaff, I do have a lot of async operations happening, this is only a small piece of my code – muudless Jul 24 '15 at 07:43
  • If you're going to down vote my answer, then maybe you can provide a better answer for a more constructive response. After all, this is suppose to be a community to help each other out :) – muudless Jul 24 '15 at 07:46