Several points :
- "Fx" to mean "function" will confuse the marrow out of people. "fn" is the conventional abbreviation.
- There's no need to wrap
$.ajax()
in$.when()
.$.ajax()
returns an inherently promise-compatible object with chainable.done()
,.fail()
,.always()
and.then()
methods. - When
$.ajax()
became promise-compatible, use of thesuccess:
callback (in the options map) became unnecessary. It remains only for backward compatibility and may one day be removed. Mixing the old approach with the new is not recommended. - the function wrapper around the simple
populateTable()
call is not necessary. You can specifypopulateTable
by name. populateTable()
must return a Promise (to a chained.then()
) in order for something to happen after its asynchronous activity has completed.
You probably want something like this :
$('#button').click(function() {
//(fn-1)getDB-Records()
$.ajax({
url: '/echo/html/'
}).then(populateTable).done(function(txt) {
//(fn-3) formatTable()
console.log('populateTable reports: ' + txt);
});
});
function populateTable(data) {
//(fn-2) just populate table without formatting
var dfrd = $.Deferred();
setTimeout(function () {
var message = 'Table populated';
console.log('populateTable says: ' + message);
dfrd.resolve(message);
}, 1);
return dfrd.promise();
}