How to allow $ q for a promise?

I use AngularJS and $ q for promises, I get confused using How can I fix this promise?

I want to return the user when calling isLoggedInPromise () or "AUTH_REQUIRED".

Thanks!

    function isLoggedInPromise() {
        $q.when(userWithAllData()).then(function(user) {
            return user;
        });
        $q.when(userWithAllData()).reject(function() {
            return "AUTH_REQUIRED";
        });
    }

    function userWithAllData(){
        var deferral = $q.defer();

        var query = new Parse.Query(Parse.User);
        query.include(["info", "info.rank", "privateInfo"]);
        query.get(Parse.User.current().id).then(function (loadedUser){
            deferral.resolve( loadedUser );
        });

        return deferral.promise;
    }
+4
source share
1 answer

What you want to do is call the resolution function in the promise when the function has all the necessary data for the actual return

in a function that returns a promise, think of “resolution” as the actual return statement.

isLoggedInPromise , . , , .

isLoggedInPromise().then(function(result){...etc...}, function(reason){//auth needed})

isLoggedin,

 function isLoggedInPromise() {
    var isLoggedIn = userWithAllData().then(
        function(user) {
            return user;
        }, 
        function(reason){
            return $q.reject("AUTH_REQUIRED")
        });    
 }

, userWithAllData.

+3

Source: https://habr.com/ru/post/1624339/


All Articles