【问题标题】:How to wait/finish multiple insert function in for each loop?如何在每个循环中等待/完成多个插入函数?
【发布时间】:2017-09-14 12:15:32
【问题描述】:

如何异步执行此操作,打印 console.log("PRINT THIS, AFTER ALL LOOP IS DONE")

我在 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");

            }

【问题讨论】:

    标签: angularjs sqlite insert promise angular-promise


    【解决方案1】:

    如果 Insert() 返回一个承诺,你可以这样做:

    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*/});
    

    【讨论】:

    • myInsert 不返回任何值,我只想插入,在插入和循环之后我想打印,“ALL IS DONE”
    • @PauloDelaCruz 如果 Insert 不返回值,则不能使用 .then() 。你确定不是吗?看起来它现在被称为 selectID,它需要返回一个承诺。
    • insertService 返回一个值,但它总是 [] 或 "null" ,......先生,您了解我的代码流程吗?你可以帮帮我吗?我只想在进入其他功能之前完成所有这个循环。
    • var select3 = SelectService3.SelectID(value.id);你会保存 select3 变量等于什么?
    • "Main" 是旧职位 id 的列表,现在,对于每个旧职位 id,SelectService1.SelectID("旧职位 id");现在我将用新的 positionID 替换它(我不把它放在那里。)并再次插入,我将在五个表中执行此操作。现在我想知道所有这些交易是否在以其他形式进行之前完成。
    【解决方案2】:

    如果每个InsertService.Insert(value)都返回promise,

    你可以用这个

    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);
           }
        });
    }
    

    然后让fnTwo, fnThree, ... 像上面一样。

    然后你可以像下面这样使用 $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 数组的所有函数都执行完毕后执行。

    您可以在所有函数完成自己的工作后打印“PRINT THIS, AFTER ALL LOOP IS DONE”。

    我想这可能对你有帮助。

    【讨论】:

    • 我的 insertService 在 sqllite 中没有返回任何值,所以我只是使用控制台来确认它是否真的在保存
    • @PauloDelaCruz 你能添加InsertService.Insert (value)的代码吗?您应该检查有关 $q 的文档。 docs.angularjs.org/api/ng/service/$q
    • this.Insert = function (data) { sql.changeDatabase("pmr_data.db"); return sql.sqlQuery("INSERT OR REPLACE INTO INTO VALUES(?,?,?,?,?,?)", UtilityService.getData(data)); }
    • @PauloDelaCruz - 你很困惑。 InsertService1.Insert(value) 显然是在返回一个承诺。如果没有,那么您尝试使用 .then() 将在您的任何代码运行之前失败。
    • @PauloDelaCruz 我更新了我的答案。请看这个。
    猜你喜欢
    • 2019-04-15
    • 2014-08-15
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2018-04-25
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多