json - Javascript timeout loop to save multiple rows to Parse.com -
i have written beforesave
script in parse.com cloud code. save results parse run through script , data modified before saving.
i have tacked in following way.
exported json parse.com dashboard.
use local json in app , run following loop:
code:
$http.get('data/full_data.json') .then(function(res) { var counter = 0; (var = 0; < res.data.results.length; i++) { setdelay(counter); savetoparse(res.data.results[i]); counter ++ }; } }); function setdelay(i) { settimeout(function() { console.log(i); }, 1000); } function savetoparse(exercise) { console.log(exercise); parsefactory.provider('exercises/').edit(exercise.objectid, exercise).success(function(data) { }).error(function(response) { $ionicloading.hide(); $rootscope.$emit('errorevent', { "message": "please check connection", "errorobject": response }); }); }
i have been trying have timeout function not exceed number of api calls allowed on parse.com.
my problem api calls done , run timeouts @ end after 1 second pause.
how can ensure each loop iteration takes timeout before looping again.
the answers work first 50 seconds , work slowly... see screen grab of network activity.
you make functions return promises. , make loop async convert recursive "loop".
$http.get('data/full_data.json') .then(function(res) { function loop(i) { if(i < res.data.results.length) { return savetoparse(res.data.results[i]).then(function() { return delay(100); }).then(function() { return loop(i + 1); }); } else return promise.resolve(res); }; return loop(0); }).then(function(res) { console.log("finished saving"); }); function delay(time) { return new promise(function(resolve, reject) { settimeout(resolve, time); }); } function savetoparse(exercise) { return new promise(function(resolve, reject) { console.log(exercise); parsefactory.provider('exercises/').edit(exercise.objectid, exercise) .success(resolve).error(function(response) { var error = { "message": "please check connection", "errorobject": response }; reject(error); $ionicloading.hide(); $rootscope.$emit('errorevent', error); }); }); }
edit: might better way. has advantage of returning promise can keep chaining promises.
$http.get('data/full_data.json') .then(function(res) { var p = promise.resolve(); res.data.results.foreach(function(elem) { p = p.then(function() { return savetoparse(elem).then(function() { return delay(100); }); }); }); return p; });
edit2: yet solution generalize async loops.
function asyncwhile(cond, body) { if(cond()) { return body().then(function() { return asyncwhile(cond, body); }); } else { return promise.resolve(); } } function asyncfor(start, end, body) { var = start; return asyncwhile(function() {return < end}, function() { return body(i++); }); } $http.get('data/full_data.json') .then(function(res) { return asyncfor(0, res.data.results.length, function(i) { return savetoparse(res.data.results[i]).then(function() { return delay(100); }); }).then(function() { return res; }); }).then(function(res) { console.log("finished saving"); });
Comments
Post a Comment