0

I'm saving three results but they are all the same. I'm running into the dam Javascript async loop problem. How do I get out of this?

my code:

 var teamMatch = function(){
     var Pairingteam = [];
     var promise = Parse.Promise.as();
     var x = matchedPairingIds.length;
     while (x--) {
         var match = matchedPairingIds[x];
         var matching = function () {
             matched.set("team", {
               __type: "Pointer",
               className: "_User",
               objectId: "yCDDEWoiwM"
               });
             matched.set("Pairing", {
              __type: "Pointer",
              className: "Pairing",
              objectId: match
              });
             Pairingteam.push(matched.save());
            }
          }
          promise.then(function(){
            return Parse.Promise.when(Pairingteam);
          });
      }
     teamMatch();

2 Answers 2

1

The issue is that matched.save(); is an asynchronous operation. Most likely your function is returning before all the saves are complete. Your best bet to overcome this is to use promises. Read up on them here: https://parse.com/docs/js_guide#promises

Then take a look at Parse JavaScript SDK and Promise Chaining and Multiple Queries Parse Javascript

Adapting the above solutions to your question would look something like this:

 var partnerMatch = function() {
 var savePromises = [];  // this will collect save promises 

// Create a trivial resolved promise as a base case.
 var promise = Parse.Promise.as();
 var x = matchArray.length // x = 3
 while (x--) {
     var matched = matchArray[x];
     var matching = function() {
         matched.set("partner", {
             __type: "Pointer",
             className: "_User",
             objectId: "yCDDEWoiwM"
         });
         matched.set("match", match);
         savePromises.push(matched.save());
     }
 }

 promise.then(function(){
   // now do the saves
   return Parse.Promise.when(savePromises);
 });
 }
 partnerMatch();

Promise.When(); waits around until all of the promises passed to it are resolved.

EDIT

I'm not sure where your matched variable is coming from, but here's the updated code

 var teamMatch = function(){
 var Pairingteam = [];
 var promise = Parse.Promise.as();
 var x = matchedPairingIds.length;
 while (x--) {
     var match = matchedPairingIds[x];
     matched.set("team", {
       __type: "Pointer",
       className: "_User",
       objectId: "yCDDEWoiwM"
       });
     matched.set("Pairing", {
      __type: "Pointer",
      className: "Pairing",
      objectId: match
      });
     Pairingteam.push(matched.save());
     }
      promise.then(function(){
        return Parse.Promise.when(Pairingteam);
      });
  }
 teamMatch();
Sign up to request clarification or add additional context in comments.

10 Comments

A little extra detail about where I put it or how to implement? Do you mean 'then'? I'm familiar with 'then' but I don't know how to implement it in this case. I'm looking up Promise.When();
I'm definitely getting promises and 3 of them as I'd expect from the savePromises array you proposed. Unfortunately it is still only saving one of these values.
Updated to include base promise
The Parse SDK is completely useless. I see the other illustrations and they're somewhat instructive but I'm not connecting the dots. The Promise.when() is a nice little trick but the example you show and the ones illustrated in the examples you provide are saving 3 identical in the array and only one parse object instead of 3 unique Parse.Objects.
Ok, it feels like I'm getting closer but now I'm getting no results in the array. I'm thinking that maybe I need to call the matching(); function? I appreciate the guidance here . . . one more step please?
|
1

The mistake was that I hoisted the matched class. Matched needs to be defined within the loop. The code should look like this:

var teamMatch = function(){
     var Pairingteam = [];
     var promise = Parse.Promise.as();
     var x = matchedPairingIds.length;
     while (x--) {
         var matched = new Parse.Object("Matched"); //THIS IS THE MISSING PIECE
         var match = matchedPairingIds[x];
         var matching = function () {
             matched.set("team", {
               __type: "Pointer",
               className: "_User",
               objectId: "yCDDEWoiwM"
               });
             matched.set("Pairing", {
              __type: "Pointer",
              className: "Pairing",
              objectId: match
              });
             Pairingteam.push(matched.save());
            }
          }
          promise.then(function(){
            return Parse.Promise.when(Pairingteam);
          });
      }
     teamMatch();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.