Skip to content

Instantly share code, notes, and snippets.

@Jeff-Lewis
Forked from refack/with-promise.js
Created March 18, 2018 18:23
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 Jeff-Lewis/f9ebdcde9d03d23f7a8a1d273e661c2d to your computer and use it in GitHub Desktop.
Save Jeff-Lewis/f9ebdcde9d03d23f7a8a1d273e661c2d 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