Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active December 21, 2017 01:46
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 mikermcneil/99396f353ef5cee4a52dd45cc44fca6b to your computer and use it in GitHub Desktop.
Save mikermcneil/99396f353ef5cee4a52dd45cc44fca6b to your computer and use it in GitHub Desktop.
that thing everyone needs now, if you're using `await` on the front-end. Obvious caveat: Don't actually use this without babel et al
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// UPDATE:
//
// If you're using a Sails app, and you need to do this on the backend, then instead of the code below,
// consider just using `sails.helpers.flow.pause()` from sails-hook-organics.
// (Now available by default in the "Web App" template.)
//
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* pause()
*
* Pause for the specified number of milliseconds.
*
* ```
* var pause = parasails.require('pause');
*
* // Wait 2 seconds
* await pause(2000);
* ```
*
* > This is just a simple wrapper around setTimeout().
* > It _cannot_ be canceled. If you need to cancel this timer,
* > then just use the native setTimeout() instead.
* -----------------------------------------------------------------
* @param {Number} ms
* -----------------------------------------------------------------
*/
parasails.registerUtility('pause', (ms)=>{
// We also could have used `parley` for a bit more consistency with the way you
// call helpers, etc. the Sails backend-- but we'd have to browserify it and all that.
// This is good enough. Since a Promise is a "thenable", it means we can use `await`!
return new Promise((resolve) => {
setTimeout(()=>{
resolve();
}, ms);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment