Skip to content

Instantly share code, notes, and snippets.

@Debdut
Created February 3, 2022 13:00
Show Gist options
  • Save Debdut/23de47e5c3d97d15aff399354f1fdd67 to your computer and use it in GitHub Desktop.
Save Debdut/23de47e5c3d97d15aff399354f1fdd67 to your computer and use it in GitHub Desktop.
JS Utils
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