var log = function(content){ $('<p/>').html(content).appendTo('#log'); } function doTask1(add, multiply){ setTimeout(function(){ var rslt = Math.floor(Math.random() * 10); log("Task 1 returned " + rslt); if(rslt >= 5) add(rslt); else multiply(rslt); }, 500); } function addTask(i, next){ setTimeout(function(){ var rslt = Math.floor(Math.random() * 10); log("addTask finished with " + rslt); next(i + rslt); }, 500); } function multiplyTask(i, next){ setTimeout(function(){ var rslt = Math.floor(Math.random() * 10); log("multiplyTask finished with " + rslt); next(i * rslt); }, 500); } $(function(){ doTask1(function(rslt){ addTask(rslt, function(final){ log("All tasks finished with " + final); }); }, function(rslt){ multiplyTask(rslt, function(final){ log("All tasks finished with " + final); }); }); });
<div id="log"></id>