Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Last active May 15, 2017 19:52
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 apaleslimghost/afb330918f6b781b611fe53ef7757861 to your computer and use it in GitHub Desktop.
Save apaleslimghost/afb330918f6b781b611fe53ef7757861 to your computer and use it in GitHub Desktop.
const addHrtime = require('@quarterto/add-hrtime');
const timers = new Set();
exports.setHrTimeout = (fn, args, time) => {
const end = addHrtime(process.hrtime(), time);
const id = timers.size;
timers.add(id);
function loop() {
const [s, ns] = process.hrtime(end);
if(!timers.has(id)) return;
if(s >= 0 && ns >= 0) {
fn(...args);
exports.clearHrTimeout(id);
} else {
setImmediate(loop);
}
}
setImmediate(loop);
return id;
};
exports.clearHrTimeout = id => timers.delete(id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment