2

In angularjs I am trying to execute services one after the other in a sequential manner. Below is my code,

$q.when()  
    .then(function () { 
            console.log("Start"); 
            return getMyDetailsService.MyDetails();                     
        })  
    .then(function (MyDetails) {
            console.log("Step 2"); 
            var AllTabs = [];               
            angular.forEach(MyDetails, function(value, key){                
                AllTabs.push({ "TabName":value.checkfile });                
            });             
        return AllTabs;         
        })  
    .then(function (AllTabs) {
            console.log("Step 3"); 
                angular.forEach(AllTabs, function(value, key){
                    console.log(value.TabName);
                    return getMongodbDataService.MongoDatadb(value.TabName)
                    //console.log(MongoDatadb)
                })      
        })
    .then(function (MongoDatadb) { console.log("Step 4"); console.log(MongoDatadb); })
    .then(function (AllTabs) { console.log("Finish"); }) 

The problem is, i am getting "undefined" after "Step 4" in console. It seems the service "getMongodbDataService" not taking the input properly. If i comment forEach loop and execute with hard code input, it works.

//angular.forEach(AllTabs, function(value, key){
      //console.log(value.TabName);
      return getMongodbDataService.MongoDatadb('Tab01')
      //console.log(MongoDatadb)
//})

Please suggest , where i am making mistake in promises handling. Please also tell me if service returns multiple "MongoDatadb" responses, will the next function (Step 4 one) process all request or will take only the last one. As per my need, i have to process all the response one by one.

Many Thanks

usersam
  • 1,125
  • 4
  • 27
  • 54
  • I might checkout his post https://stackoverflow.com/questions/20100245/how-can-i-execute-array-of-promises-in-sequential-order – Niles Tanner Sep 01 '17 at 13:35
  • Step 3 returns nothing. [`angular.forEach`](https://docs.angularjs.org/api/ng/function/angular.forEach) just invokes the iterator function once for each item, `return` inside of it actually does nothing in terms of returning result for your promise chain. – Stanislav Kvitash Sep 01 '17 at 13:36
  • Try to make it `return AllTabs.map(function(tab){ return getMongodbDataService.MongoDatadb(tab.TabName) });` instead of `forEach`. – Stanislav Kvitash Sep 01 '17 at 13:39
  • @sand were you able to resolve your issue? – Stanislav Kvitash Sep 01 '17 at 14:12
  • No @Stanislav , I tried "return AllTabs.map(function(tab){ return getMongodbDataService.MongoDatadb(tab.TabName) });" but that gives below in console.(2) [Promise, Promise] 0:Promise $$state: status:1 value:Array(1) 0:{Information Time: "98.441382"} length:1 __proto__:Array(0) __proto__:Object __proto__:Object 1:Promise {$$state: {…}} – usersam Sep 01 '17 at 17:39
  • @sand oh I see. Can you try `return $q.all(AllTabs.map(function(tab){ return getMongodbDataService.MongoDatadb(tab.TabName) }));`? [`$q.all()`](https://docs.angularjs.org/api/ng/service/$q#all) combines multiple promises into a single promise that is resolved when all of the input promises are resolved. – Stanislav Kvitash Sep 01 '17 at 17:46
  • @sand Great, will add as an answer so it wouldn't be lost in the comments. You can accept it if it works. – Stanislav Kvitash Sep 01 '17 at 17:57
  • @Stanislav It worked. Thanks a ton. You made me day. :-D – usersam Sep 01 '17 at 17:57
  • @sand accepting the answer would be greatly appreciated :) – Stanislav Kvitash Sep 04 '17 at 11:04
  • @Stanislav, how to accept the answer. I already have clicked on Up arrow. Please tell me if i am missing something. Would be a learning for me. – usersam Sep 04 '17 at 11:27
  • @sand There should be a gray check mark under the arrows :) – Stanislav Kvitash Sep 04 '17 at 11:39
  • Done !!. Thanks for the tip. I would be considering this in future answers. – usersam Sep 04 '17 at 11:43

2 Answers2

1

angular.forEach will not return any value on "Step 3". You should use map() to return an array of promises and $q.all() to combine them all into a single promise that will be resolved when all of the input promises are resolved:

return $q.all(AllTabs.map(function(tab){ return getMongodbDataService.MongoDatadb(tab.TabName) }));
Stanislav Kvitash
  • 4,614
  • 18
  • 29
0

It because you return command is in the scope of 'foreach' method not in the scope of your promise treatment.