Skip to content

Instantly share code, notes, and snippets.

@snakeneedy
Created May 21, 2020 04:03
Show Gist options
  • Save snakeneedy/282828ea81b0df3237b2a3a6e5561561 to your computer and use it in GitHub Desktop.
Save snakeneedy/282828ea81b0df3237b2a3a6e5561561 to your computer and use it in GitHub Desktop.
Get unique timestamp as number in JavaScript.
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