Skip to content

fix for methods with callbacks #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = {
}
, _done = function () {
var args_ = Array.prototype.slice.call(arguments)
, ret, total_, current_, next_, done_, postArgs;
, ret, total_, current_, next_, done_, postArgs, callbackArgs;

if (_current === _total) {

Expand All @@ -74,14 +74,17 @@ module.exports = {
return currPost.apply(self, postArgs);
} else if (typeof lastArg === 'function'){
// All post handlers are done, call original callback function
return lastArg.apply(self);
return lastArg.apply(self, callbackArgs);
}
};

// We are assuming that if the last argument provided to the wrapped function is a function, it was expecting
// a callback. We trap that callback and wait to call it until all post handlers have finished.
if(typeof lastArg === 'function'){
args_[args_.length - 1] = once(next_);
args_[args_.length - 1] = once(function () {
callbackArgs = Array.prototype.slice.call(arguments)
next_(callbackArgs);
});
}

total_ = posts.length;
Expand Down