javascript - What comes first .always() or .then() callbacks in jQuery? -
this question has answer here:
if have function has both .then
, .always
callbacks, 1 executed first?
taken deferred.resolve()
documentation:
when deferred resolved, donecallbacks added deferred.then() or deferred.done() called. callbacks executed in order added.
example below:
var $logger = $("#logentry"); function addlog(content){ $logger.append($("<li/>").html(content)); } var newpromise = $.deferred(); $.when(newpromise).done(function() { addlog("1st $when().done!"); }); newpromise.then(function() { addlog("1st then!"); }).always(function() { addlog("1st always!"); }).done(function() { addlog("1st done!"); }).done(function() { addlog("2nd done!"); }).always(function() { addlog("2nd always!"); }).then(function() { addlog("2nd then!"); }); $.when(newpromise).done(function() { addlog("2nd $when().done!"); }); addlog("resolving promise!"); newpromise.resolve();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul id="logentry"></ul>