Search code examples
javascriptjqueryajax.when

jquery "$.when" function not working as expected


I am attempting to use the jquery $.when() function to prevent certain functions from being fired until the ajax completes. I have the below function but the deferred functions are still firing at the same time as the ajax call.

code:

 $.when(cdeckDataStore("param1","param2","param3")).done([function1("param1"),function2("param1")]);


function cdeckDataStore(action,step,checked) {
   return $.ajax({
      type: "POST",
      datatype: "json",
      url: "url/to/api",
      data: {"action":action,"step":step,"data": checked},
      success: function(data) {
        console.log("success");
      }
   });

}

according to the docs function1 and function2 should wait until the ajax returns to fire.

am I missing something?


Solution

  • Nope, according to docs the references passed to .done() will be invoked after $.when() is done.

    Whereas you're invoking your functions in place function1("param1")

    You could pass a reference to a function using something like:

    function1.bind(this, 'param1')