Last active
December 25, 2017 06:20
-
-
Save unstppbl/6fbb8af97f094a7f84d98da7ef43139d to your computer and use it in GitHub Desktop.
Synchronous and asynchronous python-like sleep function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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