Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim vdeturckheim/wrapAsync.js Secret
Last active Nov 12, 2017

Embed
What would you like to do?
const wrapAsync = function (orig, name) {
return async function () {
const uuid = Uuidv4();
name = name || `mongoose.${this.op}`; // mongose Query.exec specific
const finish = function () { // this method will be called after the wrapped method gets executed
PerfHook.performance.measure(`${name}-${uuid}`, `start-${uuid}`, `end-${uuid}`);
PerfHook.performance.clearMarks(`start-${uuid}`);
PerfHook.performance.clearMarks(`end-${uuid}`);
PerfHook.performance.clearMeasures(`${name}-${uuid}`); // we remove the marks and the measure to prevent a memory leak here.
};
try {
PerfHook.performance.mark(`start-${uuid}`); // start measuring time
const res = await orig.apply(this, arguments); // calling original method
PerfHook.performance.mark(`end-${uuid}`);
finish();
return res;
}
catch (err) {
PerfHook.performance.mark(`end-${uuid}`);
finish();
throw err;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.