Created
February 3, 2022 13:00
-
-
Save Debdut/23de47e5c3d97d15aff399354f1fdd67 to your computer and use it in GitHub Desktop.
JS Utils
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 = (millis, throwOnAborted = false) => { | |
let timeout_id | |
let rejector | |
const prom = new Promise((resolve, reject) => { | |
rejector = throwOnAborted ? reject : _ => resolve() | |
timeout_id = setTimeout(() => { | |
resolve() | |
}, millis) | |
}) | |
prom.abort = () => { | |
clearTimeout(timeout_id) | |
rejector('aborted') | |
} | |
return prom | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment