Skip to content

Instantly share code, notes, and snippets.

@diegovgsilva95
Created February 4, 2025 16:13
Show Gist options
  • Select an option

  • Save diegovgsilva95/22b30eb28485b521ec06cc5979d9e1a9 to your computer and use it in GitHub Desktop.

Select an option

Save diegovgsilva95/22b30eb28485b521ec06cc5979d9e1a9 to your computer and use it in GitHub Desktop.
JS - Waiting for a specific datetime
async function waitUntilDatetime(timestamp, maxWait = 86400000) {
let beginAt = Date.now()
// Something scheduled should wait less than 24 hours, or 86400000 ms.
// After that point, it'll take too damn long, even for a daemon, so it should be refused.
if((timestamp - beginAt) > maxWait) // Refuse
throw new Error(`Requested to wait for ${timestamp - beginAt} ms., which exceeds the limit of ${maxWait} ms.`)
while(Date.now() < timestamp)
await sleep(Math.min(timestamp - Date.now(), 10000))
return (Date.now()-beginAt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment