qwerty
qwerty

Reputation: 229

How to make jQuery callback like .done in AJAX?

How to make jQuery callback like .done in AJAX?

I need to do like it:

myMethod(args)
    .done(function(args) {
        console.log(true);
    });

Thanks in advance.

Upvotes: 0

Views: 42

Answers (2)

dknaack
dknaack

Reputation: 60468

Not really clear what you want to do exactly. You can use $.ajax to make the ajax call and provide a function after the request is done. Create your own function and pass in a callback.

Sample

function doRequest(callback) {
    $.ajax({
      url: "yourUrl",
    }).done(callback));
}

doRequest(function(data) {
  // do something
});

More Information:

Upvotes: 0

Johan
Johan

Reputation: 35194

A bit unclear what you're asking. Is this what you want?

function myMethod(){

    var dfd = $.Deferred();

    // simulate something async:
    setTimeout(function(){
        dfd.resolve({foo: true}); // pass some dummy data
    }, 500);

    return dfd.promise();
}

myMethod().done(function(args){
    console.log(args.foo);
});

http://jsfiddle.net/9MRr9/1/

Read more about deferred objects here: http://api.jquery.com/category/deferred-object/

Upvotes: 2

Related Questions