angularjs - How to wait/finish multiple insert function in for each loop? -
how asynchronously, print console.log("print this, after loop done")
i have foreach inside foreach.
insertservice not return value, want insert here.
i don't know how apply $q , $q.all. pls help.
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 loop done"); }
if insert() returns promise can this:
var inserts = []; inserts.push(insertservice1.insert(value)); inserts.push(insertservice2.insert(value)); inserts.push(insertservice3.insert(value)); $q.all(inserts).then(()=>{/*once have finished*/});
Comments
Post a Comment