Ghobs
Ghobs

Reputation: 859

Parse.Promise not running after it's created

Basically what I'm trying to do here is create matchCenterComparisonPromise as a promise in Parse in the event that eBayResults.length is greater than 0. console.log('amil eh'); is logged out, so I know for a fact that it is indeed greater than 0.

However, matchCenterComparisonPromise doesn't seem to run, despite the fact that I call return matchCenterComparisonPromise; afterwards. Did I not place that return call in the right place, or am I making a different error that's causing it not to run?

function matchCenterComparison(eBayResults) {   

  console.log('eBayResults are the following:' + eBayResults);

  if (eBayResults.length > 0) {
      console.log('amil eh');

    var matchCenterComparisonPromise = new Parse.Promise(function(){
    console.log('yes the ebay results be longer than 0');

    //Query users MComparisonArray with these criteria  
    var mComparisonArray = Parse.Object.extend("MComparisonArray");
    var mComparisonQuery = new Parse.Query(mComparisonArray);
    mComparisonQuery.contains('Name', 'MatchCenter');
    //mComparisonQuery.contains("MCItems", eBayResults);

    console.log('setup query criteria, about to run it');

    mComparisonQuery.find({
      success: function(results) {
      console.log('MatchCenter comparison results :' + results);
      // No new items                      
      if (results.length > 0) {
        console.log("No new items, you're good to go!");
      }
      // New items found
      else if (results.length === 0) {
        console.log('no matching mComparisonArray, lets push some new shit');
        //replace MCItems array with contents of eBayResults
        Parse.Object.destroyAll(mComparisonArray);

        var newMComparisonArray = new mComparisonArray();
        newMComparisonArray.set('Name', 'MatchCenter');
        newMComparisonArray.set('MCItems', eBayResults);
        //newMComparisonArray.set("parent", Parse.User());

        console.log('yala han save il hagat');
        // Save updated MComparisonArray  
        newMComparisonArray.save().then({
        success: function() {
          console.log('MComparisonArray successfully created!');
          //status.success('MComparisonArray successfully created!');
        },
        error: function() {
          console.log('nah no MComparisonArray saving for you bro:' + error);
          //status.error('Request failed');
        }
        });
        //send push notification

      }
      console.log('MatchCenter Comparison Success!');
      },

      error: function(error) {
      console.log('nah no results for you bro:' + error);
      }
    });
    });

      return matchCenterComparisonPromise;

  }       
}

Upvotes: 1

Views: 58

Answers (1)

Timothy Walters
Timothy Walters

Reputation: 16874

I'm not sure you can construct a promise like that (at least I've never seen one made like that).

What I have done is create a promise and then refer to it from other code:

var myPromise = new Parse.Promise();

if (someExpression) {
    // do some work, possibly async
    myPromise.resolve(output);
} else {
    myPromise.reject({ message: 'No work done, expression failed' });
}
return myPromise;

That's the basic pattern, extend as needed.

Upvotes: 1

Related Questions