Extracting return value from nested callback function

前端 未结 2 751
清酒与你
清酒与你 2021-01-28 19:47

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.

2条回答
  •  滥情空心
    2021-01-28 20:34

    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);
        }
    }
    

提交回复
热议问题