Use promises inside the service, but DO NOT return a promise

So, I create a service that sends a message to the server to check if a specific email address is in the database.

userDB.emailExists = function()
{

    var returnVal;

    $http.get('?backend=users&email=myemail%40mydomain.com')
         .success(function(data) { returnVal = data.count ? true : false; })
         .error(function(data) { /* TODO error handling */ });

        returnVal = data.count ? true : false;
    });

    return returnVal;
};

Now there is currently a unit test for this (testing to check if a function returns trueor false) s Expected undefined to be truthy.. I seem to understand why. since the promise returns a "placeholder", so the moment we get to the statement "return" returnValis not yet set.

The only problem is that I do not know how to fix it; I don’t want to return a promise because it seems like an unnecessary layer of complexity when the result is simple true or false

+1
1

, , ; , ,

( ) - , async i.o Angular JavaScript . . .

, , , true/false, true/false .

true/false, $http:

userDB.emailExists = function() {
    return $http.get('?backend=users&email=myemail%40mydomain.com')
         .then(function(r) { return Boolean(r.data.count); });
};
+3

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


All Articles