0

With a slight modification, I am attempting to use the code provided by Bergi in jQuery Recursive AJAX Call Promise. In my case I make an AJAX call to test if a username is already used. If it is already in use then compose a new username and test that one. Once we have a username that is not in use then we are done and return that unused username. However, I am not getting the expected return value. The return value I get is undefined. The console log statement:

console.log("Return => " + username);

just before the return from the requestUsername function shows that I am returning a good value, but it is not making it to the:

requestUnused().done(function(unused_uname)

statement. Here is my code:

$(document).ready(function() {
    function request(query_val) {
        // return the AJAX promise
        return $.ajax({
            url: "/php/is_dup_ad_json.php",
            method: 'GET',
            dataType: 'json',
            data: {
                    query: query_val, sid: Math.random()
                },
        });
    }

    function requestUsername(username) {
        console.log("Initial => " + username);
       return request(username).then(function(ajax_json){
        $.each(ajax_json, function(key, value) {
                $.each(value, function(k, v) {
                if ((k == "duplicate") && (v > 0)) {
                    // try again with a different username
                    var first_initial = fname.substr(0,1);
                      var surname       = lname.substr(0,6);
                      var idx           = v + 1;
                      var tmpUname      = surname + first_initial + idx;
                      console.log("Temp => " + tmpUname);
                    return requestUsername(tmpUname);
                }
                else {
                    console.log("Return => " + username);
                    return username;
                }
              });
           });
        });
    }

    function requestUnused(){
        var fname = "bugs";
        var lname = "bunny";
        var first_initial = fname.substr(0,1);
        var surname       = lname.substr(0,7);
        var init_uname    = surname + first_initial;

        return requestUsername(init_uname);
    }

    $("#test").on('click', function() {
        requestUnused().done(function(unused_uname) {
            console.log("Done => " + unused_uname);
        });
    });
});
Community
  • 1
  • 1
Brian G
  • 399
  • 1
  • 5
  • 12
  • 3
    *"If it is already in use then compose a new username and test that one. Once we have a username that is not in use then we are done and return that unused username."* - This is not a good strategy. Let the server figure out an unused username and return that with the initial Ajax call. Do not create a system that sends Ajax back and forth endlessly for this use case, that's silly. – Tomalak Nov 18 '16 at 13:37
  • Uh, inserting `$.each` is a lot more than a "slight modification". Especially since your `then` callback doesn't `return` anything any more. – Bergi Nov 18 '16 at 14:21
  • Can you please post an example of `ajax_json`? It's not clear what those loops are good for at all. – Bergi Nov 18 '16 at 14:22
  • example of ajax_json: [{"duplicate":1}] – Brian G Nov 18 '16 at 14:36

1 Answers1

0

Without debugging tools at hand, I would guess that the return value from "requestUnused()," which is a ".then" returned from "requestUsername" is competing with the ".done". I believe ".done" and ".then" serve a similar purpose. If you want to keep a modular approach, separating the functions as it were, you could define the function in the ".then" externally and remove "requestUsername" entirely. Then (no pun intended) call "request" directly in "requestUnused," applying the ".then" functionality extracted previously in the ".click" function instead of ".done."

Alternatively, you could simply call "requestUnused()" in the click function without a ".done".

גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
  • Unfortunately I don't have the same understanding of ".then" that you have so I don't understand, "define the function in the ".then" externally and remove "requestUsername" entirely". Also, when you say as an alternative call "requestUnused()" without ".done" do you mean call as follows? requestUnused(function(unused_uname) { – Brian G Nov 21 '16 at 14:45
  • @BrianG no, I meant simply `requestUnused()` as a call. – גלעד ברקן Nov 21 '16 at 15:47
  • @BrianG by "externally," I meant that the function in the .then - `.then(function(...){...})` so the whole part from "`function`" to where it ends, which is the enclosing brace `}`. That function can be defined by itself, like `myFunction = function(...[cut and paste that code...]}`. Then you can call `myFunction(appropriate variable)` wherever you want, including as a callback in a .then. But it sounds like you may need more practice with learning to think about return values of these functions first. – גלעד ברקן Nov 21 '16 at 17:59