Here is a very simple example of a function that "promises to proceed when a given time elapses". It does that by creating a new Deferred
object, that is resolved later and returning the Deferred's promise:
function waitPromise(milliseconds){
// Create a new Deferred object using the jQuery static method
var def = $.Deferred();
// Do some asynchronous work - in this case a simple timer
setTimeout(function(){
// Work completed... resolve the deferred, so it's promise will proceed
def.resolve();
}, milliseconds);
// Immediately return a "promise to proceed when the wait time ends"
return def.promise();
}
And use like this:
waitPromise(2000).then(function(){
console.log("I have waited long enough");
});