Skip to content Skip to sidebar Skip to footer

How Can I Change The Priority For Calls In My Parse Server In Javascript?

I'm building a webfrontend and the user should be able to select some pictures and delete all of them at a time. So I'm trying it with this: function deletePic(){ var inputs =

Solution 1:

Use Promises for handle the requests. An AJAX request is async, so you must handle it asynchronyosly

var imgPromises = [];
for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].checked == true){
        var imgForDelPromise = newPromise(function (resolve) {
            userPics_query.get(inputs[i].id, {
                success: function(picForDelete) {
                    resolve(picForDelete);
                },
                error: function(picForDelete, error) {
                    alert("Error: " + error.code + " " + error.message);
                }
            });
        });
        imgPromises.push(imgForDelPromise);
    }
}

Promise.all(imgPromises).then(function (imgs) {
    destroyItAll(imgs);
});

Solution 2:

You need to handle the asynchronous nature of get requests. The easiest solution is to wrap your calls in a Promise, counting the tasks and resolve the Promise when all tasks are done.

I don't have all your code, so this is pseudo code, but it should illustrate the point:

functiondeletePic() {
  var inputs = document.querySelectorAll("input[type='checkbox']");
  varPicsForDelete = [];
  // Promisevar promise = newPromise(function(resolve, reject) {
    var fetching = 0;
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].checked == true) {
        //Count asynchronous tasks
        fetching++;
        userPics_query.get(inputs[i].id, {
          success: function(picForDelete) {
            PicsForDelete.push(picForDelete);
            alert(picForDelete.id + " " + PicsForDelete[i]);
            //Mark asynchronous task as done
            fetching--;
            //If all tasks done, resolveif (fetching == 0) {
              resolve();
            }
          },
          error: function(picForDelete, error) {
            alert("Error: " + error.code + " " + error.message);
          }
        });
      }
    }
    //If no tasks registered, resolve immidiatelyif (fetching == 0) {
      resolve();
    }
  }).then(function() {
    //When all asynchronous tasks is completed, call "destroyItAll"destroyItAll(PicsForDelete);
  });
}

Post a Comment for "How Can I Change The Priority For Calls In My Parse Server In Javascript?"