Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Created February 17, 2014 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasBurleson/9055159 to your computer and use it in GitHub Desktop.
Save ThomasBurleson/9055159 to your computer and use it in GitHub Desktop.
Example of using promise chains to delay and log messages. Uses Q Promises with setTimeout to create promise-based API delay() function.
/**
* Chain 2 Delay calls together in a non-nesting fashion
*/
function myDelayedMessages()
{
/**
* Delay function using Q promises and setTimeout() internally
*/
var delay = function( duration )
{
var dfd = Q.defer();
setTimeout(function ()
{
dfd.resolve( "Slept for "+time );
}, time);
return dfd.promise;
},
/**
* Function to wait <XX> msecs and log response to Console
*/
waitAndLog = function( duration )
{
return function()
{
return delay( duration ).then( function( msg )
{
console.log( msg );
});
};
};
waitAndLog(1000)().then( waitAndLog(2000) );
}
@ThomasBurleson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment