Unexpected behavior with asynchronous execution of sync hooks
Consider this:
const hook = funHooks();
const fn = hook('sync', (arg) => {
console.log('inner', arg);
});
fn.before((next, arg) => {
console.log('outer', arg);
setTimeout(() => {
next(arg)
})
});
fn(1);
fn(2);
The output is:
outer 1
outer 2
inner 1
inner undefined
while I'd expect the last line to be inner 2.
From the README:
Also, if you're hooking a function with sync your hooks should all call next synchronously (e.g. no ajax) so that your value can be returned. If you asynchronously call next in a sync hook then the return value will be undefined.
it's clear that this is not intended usage, but not clear that it's not just the return value that's affected. Perhaps the note should be updated to say "your hooks MUST all call next synchronously"?
yeah i agree with you. i suppose i didn't code for this very well or include any tests for this use case since it's not intended usage, but the undefined behavior does look odd. i would have expected the same output that you expected. i'll take a look and see if there's not a way to make the unexpected usage act more predictably at least, maybe add a test case. the documentation can be updated easily though for sure!