I am trying to implement a load more ajax function. However, whenever the button is clicked, it runs the function inside then() before deferred object is resolved. I had to use setTimeout in then to make it work. I don't think this is a correct way since it makes then() completely meaningless ? Can anyone take a look what's going on please? Thanks.
$(document).on('click', '.c-button--lg', function() {
var appendHTML = function () {
var $dfd = $.Deferred();
$.get(nextLink, function(html) {
$pillarContainer.append(html);
console.log(0);
$dfd.resolve();
});
return $dfd.promise();
};
appendHTML().then(function(){
console.log(1); //This is running before console.log(0);
});
});
appendHTML(), but calls a function namedappendData(). Thus, we have no idea whatappendData()actually is. In addition, you should just return the promise that$.get()already returns. There is no reason to use the promise anti-pattern of creating a new deferred here.