0

I am using Parse data, and I want to return the variable ParseUserArray. But just after it passing by the success promise. I was wondering how can I do that.

var UserWs = angular.module('UserWs', []);
    
    UserWs.service('UserWsService', ['parseInit', function(parseInit){
        var service = this;
            this.getUserAtParse = function(id){
                var user = Parse.Object.extend("User");
                var query = new Parse.Query(user);
                  var parseUserArray = [];
                    query.find({
                        success: function(anUser) {
                            for (var i = 0; i < anUser.length; i++) {
                                var newUser = new User(anUser[i]);
                                parseUserArray.push(newUser);
                            }
                            console.log(parseUserArray);
                             
                        }

                    });
                    
                    var User =function(anUser){
                        this.id = anUser.id;
                        this.name = anUser.get("name");
                        this.email = anUser.get("username");
                        this.company = anUser.get("company");
                    }
            return parseUserArray;
                    
                    
                };
4
  • stackoverflow.com/questions/14220321/… Commented Mar 24, 2017 at 22:09
  • but i am not using ajax Commented Mar 24, 2017 at 22:10
  • Why don't wrap the call to UserWsService.getUserAtParse inside $timeout? Commented Mar 24, 2017 at 22:14
  • i don't think that this will works, because i guess the function will takes longer to start, but inside will be the same thing, but i don't know, i will try! thanks :) Commented Mar 24, 2017 at 22:18

1 Answer 1

2

You can use promise for this - $q service

var UserWs = angular.module('UserWs', []);

UserWs.service('UserWsService', ['$q', 'parseInit', function($q, parseInit) {
  var service = this;
  this.getUserAtParse = function(id) {
    var defer = $q.defer();
    var user = Parse.Object.extend("User");
    var query = new Parse.Query(user);
    var parseUserArray = [];
    query.find({
      success: function(anUser) {
        for (var i = 0; i < anUser.length; i++) {
          var newUser = new User(anUser[i]);
          parseUserArray.push(newUser);
        }
        console.log(parseUserArray);
        defer.resolve(parseUserArray);
      }

    });

    var User = function(anUser) {
      this.id = anUser.id;
      this.name = anUser.get("name");
      this.email = anUser.get("username");
      this.company = anUser.get("company");
    }
    return defer.promise;
  }
}]);

UserWs.controller('sampleCtrl', ['$scope', 'UserWsService', function($scope, UserWsService) {
  UserWsService.getUserAtParse(SOME_ID).then(function(resultArray) {
    //logic here
  });
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

i really didn't know that this exists hahaha, i will have a look at documentation and try your solution too! Thanks

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.