0

Assuming I have a method wrapping the jQuery ajax method:

function GetJson(url, completeCallback, alwaysCallback, failCallback) {   
var newUrl = location.protocol + "//" + location.host + url;

$.getJSON(newUrl).done(function (result) {
    if (typeof completeCallback == "function") {
        completeCallback(result);
    }                
}).fail(function (jqxhr, textStatus, error) {
    if (typeof failCallback == "function") {
        failCallback(result);
    } else {
        alert("Request failed for " + url + " textStatus:" + textStatus + " Error:" + error);
    }        
}).always(function () {
    if (typeof alwaysCallback == "function") {
        alwaysCallback();
    }        
}); }

And i call the DoSomething method, which internally calls GetJson. The result from the GetJSON callback I want to pass as the first parameter of DoResume; the remaining arguments should be passed from the methods signature.

function DoSomething(a, b, c, id) {
var url = '/MyController/GetData?id=' + id;    
GetJson(url, DoResume(this.Result, a, b, c)); }

function DoResume(result, a, b, c) { }

I tried to achieve this using this keyword, but the result is not assigned.

4
  • What is GetJson? Commented Aug 8, 2016 at 15:31
  • Sorry, my mistake ;-) Commented Aug 8, 2016 at 15:33
  • 1
    One wonders why you have such a wrapper for $.getJSON - it seems you're hiding away the new-style promises and returning to using the likes of the success and error options you can pass to the core $.ajax function. Commented Aug 8, 2016 at 15:38
  • Thx! Yes, i know, it is very bad style. I will refactor this later to use promises, for now, i just need to get the stuff working in a short time frame ;-( Commented Aug 8, 2016 at 15:56

2 Answers 2

3

You need to pass a function not the result of its invocation

GetJson(url, function(result){
    DoResume(result, a, b, c);
});
Sign up to request clarification or add additional context in comments.

Comments

1

In your example, you are not passing DoResume into SendJSON, you're calling DoResume, then passing its result. You need to pass a function instead.

SendJSON( url, null, function( result ) {
    DoResume( result, a, b, c );
} );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.