Salvador Dali
Salvador Dali

Reputation: 222571

jQuery $.when with function

I am trying to use jQuery with deferred objects:

So when I am using

$.when(
  $.ajax("test.aspx")
).done(function(){
  console.log('1');
});

Everything works fine. 1 is shown only after ajax is executed and returned. But if I will do

function a(){
  $.ajax("test.aspx")
}

$.when(
  a()
).done(function(){
  console.log('1');
});

everything breaks apart. Any idea how to fix it using function a?

Upvotes: 1

Views: 48

Answers (1)

Ayush
Ayush

Reputation: 42450

You need your function to return the ajax promise:

function a() {
  return $.ajax("test.aspx");
}

Upvotes: 3

Related Questions