Skip to content

Instantly share code, notes, and snippets.

@refack
Created November 14, 2017 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save refack/e6d36a77cef460ca53b4e1ed58822434 to your computer and use it in GitHub Desktop.
Save refack/e6d36a77cef460ca53b4e1ed58822434 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const async_hooks = require('async_hooks');
let indent = 0;
async_hooks.createHook({
init(asyncId, type, triggerAsyncId) {
const eid = async_hooks.executionAsyncId();
const indentStr = ' '.repeat(indent);
fs.writeSync(
1,
`${indentStr}${type}(${asyncId}):` +
` trigger: ${triggerAsyncId} execution: ${eid}\n`);
},
before(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}before: ${asyncId}\n`);
indent += 2;
},
after(asyncId) {
indent -= 2;
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}after: ${asyncId}\n`);
},
destroy(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}destroy: ${asyncId}\n`);
},
promiseResolve(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}promiseResolve: ${asyncId}\n`);
},
}).enable();
var makePromise = timeout => {
return new Promise(done => {
setTimeout(() => {
console.log('done');
done();
}, timeout);
});
};
makePromise(3000).then(() => {
fs.writeFile('bar.txt', 'baz', { encoding: 'utf8' }, (e) => {
if(e) {
console.log(e);
}
else {
console.log('finished');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment