Reputation: 229
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
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.
function doRequest(callback) {
$.ajax({
url: "yourUrl",
}).done(callback));
}
doRequest(function(data) {
// do something
});
Upvotes: 0
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);
});
Read more about deferred objects here: http://api.jquery.com/category/deferred-object/
Upvotes: 2