javascript - Parse SDK saveAll promise chaining -


advice parse developer forum said "limit saveall 75 objects unless 1 wants saveall make own batches" default 20 objects. , put in promise chain.

i need saveall promise chain don't know how many promises need.

how done?

i have array of arrays. sub arrays length 75. need indexes of master array saveall in promise each.

            var savepromises = [];  // collect save promises               while((partition=partitionedarray.pop()) != null){                   savepromises.push(parse.object.saveall(partition, {                     success: function(objs) {                         // objects have been saved...                       },                     error: function(error) {                           // error occurred...                          status.error("something failed");                     }                 }));             }              return parse.promise.when(savepromises);     }).then(function() {          // set job's success status         status.success("successful everything"); 

a nice way build chain of promises recursively. if you've batched objects need saving batches, of work done already.

// assume batches [ [ unsaved_object0 ... unsaved_object74 ], [ unsaved_object75 ... unsaved_object149 ], ... ] function savebatches(batches) {     if (batches.length === 0) { return parse.promise.as(); }     var nextbatch = batches[0];     return parse.object.saveall(nextbatch).then(function() {         var remainingbatches = batches.slice(1, batches.length);         return savebatches(remainingbatches);     }); } 

edit - call this, call , handle promise returns...

function doallthosesaves() {     var batches = // code build unsaved objects     // don't save them yet, create (or update) e.g....     var myclass = parse.object.extend("myclass")     var instance = new myclass();     // set, etc     batches = [ [ instance ] ];  // see? not saved     savebatches(batches).then(function() {         // saves done     }, function(error) {         // handle error     }); } 

edit 2 - @ point, transactions want won't fit under burst limit of free tier, , spread out (somehow) won't fit within timeout limit.

i've struggled similar problem. in case, it's rare, admin-facing migration. rare enough , invisible end user, have made me lazy solid solution. kind of different question, now, few ideas solid solution be:

  1. see underscore.js _.throttle(), running client, spread transactions out on time
  2. run own node server throttles calls parse (or equal) _.throttle().
  3. a parse scheduled job runs frequently, taking small bite @ time (my case involves import file, can save initially, open in job, count number of objects i've created far, scan accordingly file, , batch)
  4. my current (extra dumb, functional) solution: admin user manually requests n small batches, taking care space requests ("one mississippi, 2 mississippi, ...") between button presses
  5. heaven forbid - hire back-end, remembering pay for, , parse -- @ free-tier -- pretty nice.

Comments