Created
May 21, 2020 04:03
-
-
Save snakeneedy/282828ea81b0df3237b2a3a6e5561561 to your computer and use it in GitHub Desktop.
Get unique timestamp as number in 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 sleepAsync = (ms) => (new Promise((resolve) => (setTimeout(resolve, ms)))); | |
const getUniqueTimstampAsync = (function () { | |
let prev; | |
return async function () { | |
let timestamp = +(new Date()); | |
while (prev === timestamp) { | |
await sleepAsync(1); | |
timestamp = +(new Date()); | |
} | |
prev = timestamp; | |
return timestamp; | |
}; | |
} ()); | |
export default getUniqueTimstampAsync; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment