Deferred functionality of jQuery

by Malin Jayakody

JavaScript

function nonBlocking() {
   var defer = $.Deferred();
   // setTimeout is a non-blocking function
   setTimeout(function() {
      alert('Non-Blocking function call after thread ends');
      defer.resolve();
   }, 0);
   return defer;
}

// Now you can chain a done or fail handler to the method call
nonBlocking().done(function() {
   alert('Non-Blocking function is done');
});

alert('End of main thread comes before the non-blocking function');