在我的函数完成之前调用status.success

时间:2014-07-29 19:08:51

标签: javascript parse-platform promise

在我的Parse后台作业中,我希望在调用processUser(user);之前调用matchCenterComparison(eBayResults);status.success函数。

我目前在matchCenterComparison(eBayResults);结尾处返回processUser(user);,如下所示。

我认为,因为它在processUser(user);中返回,并且在该函数完成之后才调用status.success,因此它会等到matchCenterComparison(eBayResults);完成后再调用它。似乎并非如此。

在Parse论坛中,我被告知我需要让matchCenterComparison函数返回一个Promise,并确保函数调用也是我的主要承诺链的一部分,以便后台作业等到它在status.success被调用之前完成。我无法找出正确的语法来实现这一点,因为我无法在userQuery.each中连续返回两个函数。

主要承诺链:

Parse.Cloud.job("MatchCenterBackground", function(request, status) {
  var usersQuery = new Parse.Query(Parse.User);

    usersQuery.each(function (user) {
        return processUser(user);
    }).then(function() {
        status.success("background job worked brah!");
    }, function(error) {
        status.error(error);
    });
});

processUser函数:

function processUser(user) {
    // ... code to setup per-user query ...
  var matchCenterItem = Parse.Object.extend("matchCenterItem");
    var query = new Parse.Query(matchCenterItem);

    // easy way to share multiple arrays
    var shared = {
        promises: [],
        searchTerms: [],
    };

    return query.find().then(function(results) {
        // process results, populate shared data (promises and searchTerms)
    console.log('matchCenterItem query results:' + results);
    if (results.length > 0) {
        console.log('we have entered the matchcenteritem query');

        for (i = 0; i < results.length; i++) {

          console.log('we have also entered the loop inside the matchCenterItem query');
          // later in your loop where you populate promises:
          var searchTerm = results[i].get('searchTerm');
          // add it to the array just like you add the promises:
          shared.searchTerms.push(searchTerm);

          url = 'http://svcs.ebay.com/services/search/FindingService/v1';
          //push function containing criteria for every matchCenterItem into promises array
          shared.promises.push((function() {

            if (results[i].get('itemLocation') == 'US') 
            {
              console.log('americuh!');
              var httpRequestPromise = Parse.Cloud.httpRequest({
                url: url,
                params: {// httprequest params in here}
              });
            } 

            else if (results[i].get('itemLocation') == 'WorldWide') 
            {
              console.log('Mr worlwide!');
              var httpRequestPromise = Parse.Cloud.httpRequest({
                url: url,
                params: {// httprequest params in here}
              });
            }

            return httpRequestPromise;
          })());
        }
      }

        //buildEbayRequestPromises(results, shared);
    }).then(function() {
        // process promises, return query promise
        return Parse.Promise.when(shared.promises).then(function() {
            // process the results of the promises, returning a query promise
            // ... code here ...
      console.log('were in the when.then of promise');

      var eBayResults = [];
      for (var i = 0; i < arguments.length; i++) {
        var httpResponse = arguments[i];
        // since they're in the same order, this is OK:
        var searchTerm = shared.searchTerms[i];
        // pass it as a param:
        var top3 = buildEbayRequestPromises(httpResponse.text, searchTerm);
        eBayResults.push(top3);
      }

      return matchCenterComparison(eBayResults);
        });
    });
}

matchCenterComparison函数:

function matchCenterComparison(eBayResults) {
  if (eBayResults.length > 0) {
    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);
      }
    });   
  }
} 

0 个答案:

没有答案