0

To simplify my question i did some modifications to the code. I am now able to get the value from the callback function i now want to pass this data into a variable.

Javascript

How can i get PostInformation to return args?

function AjaxRequest(callback) {

    var hasErrors = false;
    dojo.xhrPost({
        url: 'hello',
        content: SomeData,
        load: function (formErrors) {
            //preform some operation 
            //set hasErrors to true
            hasErrors = true;

            if (typeof callback === "function") callback(hasErrors);
        },
        error: function (e) {
            console.log(e + ' page not posted error');

        }

    });
}

function PostInformation() {
    try {
        AjaxRequest(function (args) {
            console.log('The hasErrors is  ' + args);
            return args;
        });

    } catch (e) {
        console.log(e);
    }
}
devdar
  • 5,564
  • 28
  • 100
  • 153
  • 1
    why are you using `jquery` `foreach` and `dojo` `foreach`? don't they both do the same thing? – abc123 Apr 22 '14 at 17:28
  • well i used dojo foreach to process dojo objects. This framework is new to me i had no idea i could have used jquery – devdar Apr 22 '14 at 17:33

2 Answers2

2

You're calling handleServerResponse when you send the request, not in the callback. It should be:

var doesErrorsExist = postToServer(function() {
    handleServerResponse(containers, function (args) {
        return args;
    });
});

But this still won't work -- an asynchronous function can never return a value to its caller, because the value won't exist until the operation completes after the function returns.

I haven't tried to figure out the logic of everything you're trying to do, so I don't have a concrete suggestion for how to fix that. I think if you reread the question you linked to, you should get some more insight.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

After some more test i realized i just needed the AjaxRequest function to return data at the end of the function and declare a variable equal to the AjaxRequest and use a callback function to return its value. Under is my code.

If this solution isn't the most appropriate please comment.

function AjaxRequest(callback) {

    var hasErrors = false;
    dojo.xhrPost({
        url: 'hello',
        content: SomeData,
        load: function (formErrors) {
            //preform some operation 
            //set hasErrors to true
            hasErrors = true;

            //if (typeof callback === "function") callback(hasErrors);
        },
        error: function (e) {
            console.log(e + ' page not posted error');

        }

    });

    return hasErrors;
}

function PostInformation() {
    try {
        var results = AjaxRequest(function (args) {
            //console.log('The hasErrors is  ' + args);
            return args;
        });

        return results;

    } catch (e) {
        console.log(e);
    }
}
devdar
  • 5,564
  • 28
  • 100
  • 153