I'm struggling to get deferred to work how I would like it. Here is my test scenario:
function myFn(val) {
var d = $.Deferred();
console.log("starting: " + val);
setTimeout(function() {
console.log('timeover:' + val);
d.resolve();
}, 1000);
console.log("ending: " + val);
return d.promise();
}
test = myFn(1).done(myFn(2));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The output is
starting: 1
ending: 1
starting: 2
ending: 2
timeover: 1
timeover: 2
where what I would like is:
starting: 1
timeover: 1
ending: 1
starting: 2
timeover: 2
ending: 2
Where have I misunderstood the deferred?