Last active
May 6, 2023 13:38
-
-
Save joeperpetua/c8af5edf4e62b41f28c52778f3846334 to your computer and use it in GitHub Desktop.
Async sleep function with unit parameters for JavaScript.
This file contains 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
const sleep = async (time, unit) => { | |
switch(unit){ | |
case 'ms': | |
return new Promise(resolve => setTimeout(resolve, time)); | |
break; | |
case 's': | |
return new Promise(resolve => setTimeout(resolve, time*1000)); | |
break; | |
case 'm': | |
return new Promise(resolve => setTimeout(resolve, time*60000)); | |
break; | |
case 'h': | |
return new Promise(resolve => setTimeout(resolve, time*3600000)); | |
break; | |
default: | |
throw new Error(`Sleep unit measure not recognized.\nSupported: ms, s, m, h.\nGiven: ${unit}`); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment