Created
August 3, 2020 08:16
-
-
Save im-denisenko/5e170f47791134be31ec98dae0c9ebac to your computer and use it in GitHub Desktop.
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
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