Created
February 4, 2025 16:13
-
-
Save diegovgsilva95/22b30eb28485b521ec06cc5979d9e1a9 to your computer and use it in GitHub Desktop.
JS - Waiting for a specific datetime
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
| 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