javascript - Jquery deferred not behaving as expected -


i make 2 ajax calls. second 1 should called when first finished:

var deferred = $.deferred();  firstajaxcall();  deferred.done(function () {    secondajaxcall(); });  function firstajaxcall() {   $.ajax({     url: '/someurl',     type: 'post',     success: function () {         deferred.resolve();     }   }); }  function secondajaxcall() {     $.ajax({       url: '/someotherurl',       type: 'get',     }); } 

i tried (jquery deferreds)

$.when(firstajaxcall()).done(function() {     secondajaxcall(); }); 

but no luck.

still, in first example, second call gets called first, doesn't

in first example flow this:

firstajaxcall(); secondajaxcall(); deferred.resolve(); 

why second call called first , before deferred.resolve() ?

you have return deferred $.ajax $.when make work

function firstajaxcall() {     return $.ajax({         url  : '/someurl',         type : 'post'     }); }  function secondajaxcall(data_from_first) {     return $.ajax({         url  : '/someotherurl',         type : 'get',     }); }  firstajaxcall().done(secondajaxcall); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -