2017-09-14 84 views
-1

如何异步执行此操作,请打印console.log(“打印此文件,完成所有循环后完成”)如何等待/完成每个循环的多个插入函数?

我已经forEach里面forEach。

InsertService不返回任何值,我只是想在这里插入。

我不知道如何申请$ q和$ q.all。请帮助。

angular.forEach(MAIN, function (value, key) { 

      //#1 
     SelectService1.SelectID(value.id).then(function (res) { 
      angular.forEach(res, function (value, key) { 

       InsertService1.Insert(value).then(function (res) { 
        console.log("NEW INSERTED Service 1!"); 
       }, 
       function (err) { 
       }); 

      }); 
      }, 
      function (err) { 
      }); 

      //#2 
     SelectService2.SelectID(value.id).then(function (res) { 
      angular.forEach(res, function (value, key) { 

       InsertService2.Insert(value).then(function (res) { 
        console.log("NEW INSERTED Service 2!"); 
       }, 
       function (err) { 
       }); 

      }); 
      }, 
      function (err) { 
      }); 


      //#3 
     SelectService3.SelectID(value.id).then(function (res) { 
      angular.forEach(res, function (value, key) { 

       InsertService3.Insert(value).then(function (res) { 
        console.log("NEW INSERTED Service 3!"); 
       }, 
       function (err) { 
       }); 

      }); 
      }, 
      function (err) { 
      }); 
    }); 
       // 
       console.log("PRINT THIS, AFTER ALL LOOP IS DONE"); 

      } 

回答

1

如果插入()返回一个承诺,你可以做这样的事情:

var inserts = []; 
inserts.push(InsertService1.Insert(value)); 
inserts.push(InsertService2.Insert(value)); 
inserts.push(InsertService3.Insert(value)); 

$q.all(inserts).then(()=>{/*once all have finished*/}); 
+0

myInsert不返回任何值,我只是想插入和插入和循环后,我想打印,“所有已完成” –

+0

先生我编辑我的问题请参阅。 –

+0

@PauloDelaCruz如果Insert没有返回值,则不能使用.then()。你确定它没有?它看起来像它现在所谓的selectID,它需要返回一个承诺。 – rjustin

1

如果每个InsertService.Insert(value)回报承诺,

您可以使用此

var res1 = InsertService1.Insert(value).then(function (res) { 
    console.log("NEW INSERTED Service 1!"); 
}, function (err) { 
    console.log("error here"); 
}); 

var res2 = InsertService2.Insert(value).then(function (res) { 
    console.log("NEW INSERTED service 2 !"); 
}, function (err) { 
    console.log("error here"); 
}); 

var res3 = InsertService3.Insert(value).then(function (res) { 
    console.log("NEW INSERTED service 3"); 
}, function (err) { 
    console.log("error here"); 
}); 

$q.all([res1, res2, re3]).then(function(results){ 

    console.log(results[0]); // this returns promise of res1; 
    console.log(results[1]); // this returns promise of res2; 
    console.log(results[2]); // this returns promise of res3; 

    console.log("PRINT THIS, AFTER ALL LOOP IS DONE"); 
}); 

试这:) :)

编辑

我不知道是我的理解是你想要的,这里是一个示例代码。

function fnOne(param1, param2, ...) { 
    return $q(function (resolve, reject) { 

     // you can do anything what you want in here 
     // and just put success value or error value in resolve or reject 

     if(success){ 
      // result of your code is successful 
      resolve(success value);  // this success value will call in $q.all(); 
     } else { 
      // result of your code is failed 
      reject(failed value); 
     } 
    }); 
} 

,并fnTwofnThree,......像上面。

然后你可以像下面这样使用$ q.all。

$q.all([ 
    fnOne(
     param1, 
     param2, 
     ... 
    ), 
    fnTwo(
     param1, 
     param2, 
     ... 
    ), 
    ... 
]).then(function (response) { 
    // response[0] returns promise of fnOne() 
    // response[1] returns promise of fnTwo() 
    .... 

    console.log("PRINT THIS, AFTER ALL LOOP IS DONE"); 
}); 

$q.allthen执行$q.all阵列的所有功能之后执行。

在所有功能完成其自己的工作后,您可以打印“打印完本文后,完成所有循环”。

也许这可以帮助你,我想。

+0

我的insertService不会在sqllite中返回任何值,所以我只是使用控制台来确认它是否真的节省了 –

+0

先生我编辑我的问题先生请参阅。 –

+0

@PauloDelaCruz你可以添加'InsertService.Insert(value)'的代码吗?你应该检查关于$ q的文档。 https://docs.angularjs.org/api/ng/service/$q –