Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync function return value with hooks gets messed up somewhere in the call stack ? #16

Open
bojand opened this issue Jul 2, 2014 · 2 comments

Comments

@bojand
Copy link

bojand commented Jul 2, 2014

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.

@dev0x10
Copy link

dev0x10 commented Nov 4, 2015

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.

@bojand
Copy link
Author

bojand commented Nov 4, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants