Parse.Promise and angluarjs $q promise

689 views Asked by At

I am trying to use Parse sdk with angularjs and I am a bit confused on handling promises when using implementation of promises in parse and angular.js

Parse promise provides a method fail to catch error where as angularjs provides us with catch. So say if I am inside Parse promise object and have to return say angularjs promise from it or vice versa. It won't have I guess fail method in it

ParsePromise().then(function(){
  return angularPromise();
}).fail(function (error){

});

AngularPromise().then(function(){
  return ParsePromise();
}).catch(function (error){

});

What should I do in such scenario?

2

There are 2 answers

2
Nikolay Rusev On

I will suggest to use $q. Just wrap parse promises in angular's $q promises. then use only angularjs promise handling.

  findByIdsInParse = function(arr) {
            var ParseObject = ParseService.Object.extend('ParseObject');
            var deffered = $q.defer();
            var query = new ParseService.Query(ParseObject);
            query.containedIn('objectId', arr);
            query.find({
                success: function(data) {
                    deffered.resolve(data);
                },
                error: function(error) {
                    deffered.reject(error);
                }
            });

            return deffered.promise;
        };
0
phuwin On

We can wrap a parse function into a return $q((resolve,reject)=>{}) then we can use them in all $q functions. So something like this:

findTransactionById(id){
  return $q((resolve,reject) => {
    //... set up variables, queries ...
    query.find().then(resolve,reject)
  })
}

//we can then use them in all $q functions
findTransactionById(12).
  then(console.log)

let promises = [findTransactionById(10),findTransactionById(9)]
$q.all(promises)
  .then((values) => {
    return findTransactionById(999)
  })
  .catch(console.error)