Skip to content

Instantly share code, notes, and snippets.

@im-denisenko
Created August 3, 2020 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save im-denisenko/5e170f47791134be31ec98dae0c9ebac to your computer and use it in GitHub Desktop.
Save im-denisenko/5e170f47791134be31ec98dae0c9ebac to your computer and use it in GitHub Desktop.
export default function(awaitable, microseconds){
return new Promise(async (resolve) => {
const start = (+new Date);
if ('function' === typeof awaitable) {
await awaitable();
} else {
await awaitable;
}
const spent = (+new Date) - start;
if (spent > microseconds) {
resolve();
} else {
setTimeout(() => { resolve(); }, (microseconds - spent));
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment