Javascript aysnc promise

Hi,

I have the following function in a separate js file that is used throughout the app for logging in:

function login(username, password, server) {
    var result = "Error";
    var url = "http://" + server + "/enteliweb/api/auth/login?username=" + username + "&password=" + password;
    $.ajax({
        method: "GET",
        url: url,
        dataType: 'xml',
        success: function (xml, status, jqXHR) {
            $(xml).find('String').each(function(idx, item) {
                result = ($(item).attr("value"));

            });
        },

        error: function(error, test1, test2) {
            result = "Failed";
        }


    });
    return result;
}

Open in new window


I've tried the following 2 methods to only fire the alert notification when the login has compelted (success function called) but it always fires immediately.

I'm sure there is something I'm missing as I'm new to JS, hoping someone can point it out to me.

$.when(login(this.username(), this.password(), this.serverAddress())).done(function (data, textStatus, jqXHR) {
                alert(jqXHR.status); // Alerts 200
            });
            var result = login(this.username(), this.password(), this.serverAddress()).done(function(test) {
                var type = result === 'OK' ? 'success' : 'error';
                DevExpress.ui.notify(result, type);
                if (result === 'OK') {
                    ACUView.app.navigate({
                        view: "home"
                    });
                }
            });

Open in new window

LVL 1
wint100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
$.when() expects a deferred object, not a string.  You are incorrectly assigning it the return value of login, which is a String object.
...the jqXHR object returned by jQuery.ajax() is a Promise-compatible object...
https://api.jquery.com/jquery.when/
So, return $.ajax() instead.

function login(username, password, server) {
    var result = "Error";
    var url = "http://" + server + "/enteliweb/api/auth/login?username=" + username + "&password=" + password;
    return $.ajax({
        method: "GET",
        url: url,
        dataType: 'xml',
        success: function (xml, status, jqXHR) {
            $(xml).find('String').each(function(idx, item) {
                result = ($(item).attr("value"));

            });
        },

        error: function(error, test1, test2) {
            result = "Failed";
        }


    });
  
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wint100Author Commented:
Great, thanks. One more thing, how do I get the result of the GET from the success function when calling in my app code:

I need to get the result in my $when callback.

success: function (xml, status, jqXHR) {
            $(xml).find('String').each(function(idx, item) {
                result = ($(item).attr("value"));

            });
        },
0
wint100Author Commented:
Got it, it's all in the 'data' parameter.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.