Skip to content

Instantly share code, notes, and snippets.

@unstppbl
Last active December 25, 2017 06:20
Show Gist options
  • Save unstppbl/6fbb8af97f094a7f84d98da7ef43139d to your computer and use it in GitHub Desktop.
Save unstppbl/6fbb8af97f094a7f84d98da7ef43139d to your computer and use it in GitHub Desktop.
Synchronous and asynchronous python-like sleep function
// Sycnhronous python-like sleep function
const sleep = (ms) => {
const start = new Date().getTime();
const expire = start + ms;
while (new Date().getTime() < expire) {
// spinning...
}
};
// Asynchronous sleep
const sleep = time => new Promise(_ => setTimeout(_, time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment