hooks-js
hooks-js copied to clipboard
Sync function return value with hooks gets messed up somewhere in the call stack ?
With 0.3.2 and 0.2.1
var hooks = require('hooks');
function Doc(data) {
this.name = data.name;
}
// Add hooks' methods: `hook`, `pre`, and `post`
for (var k in hooks) {
Doc[k] = hooks[k];
}
Doc.prototype.foo = function() {
console.log('foo');
this.name = this.name.toUpperCase();
};
Doc.prototype.getBar = function() {
console.log('bar');
return 'bar';
};
Doc.pre('foo', function(next) {
console.log('pre foo');
next();
});
Doc.pre('getBar', function(next) {
console.log('pre getBar');
next();
});
var doc = new Doc({name: 'Joe'});
doc.foo(); // good
console.log(doc.getBar()); // prints return value of undefined
console.dir(doc); // { name: 'JOE' }
Output:
pre foo
foo
pre getBar
bar
undefined
{ name: 'JOE' }
Maybe I am doing something wrong...? Everything is executed correctly, but the return value ends up being undefined
. Tried stepping through things and debugging it, not sure what happens. The return value is correct both in the ret
in _done
and in fnWrapper
in once
.
Your output looks ok, but with additional output 'undefined'.
The problem is you call doc.getBar() inside a console. But inside your doc.getBar() method there is another console.
Simply try this : console.log(console.log("JOE")); You will see it will print undefined.
doc.getBar()
prints 'bar'
and then returns
'bar'
. so the return value should be printed.
ie.
function getBar() {
console.log('bar');
return 'bar';
}
console.log(getBar());
outputs:
bar
bar