Skip to content

Commit

Permalink
Fix thunkify to work when called multiple times
Browse files Browse the repository at this point in the history
The thunkify definitions as published will only work the first time they are called, after which the thunkory will continue appending to a shared args array, calling `fn` with more and more arguments.

To prevent this, the thunkory should clone a new copy of args every time it is called, and push the latest cb to that args array.
  • Loading branch information
airandfingers committed May 9, 2015
1 parent 062b377 commit 594669c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions async & performance/ch4.md
Expand Up @@ -1755,10 +1755,11 @@ Consider:

```js
function thunkify(fn) {
var args = [].slice.call( arguments, 1 );
var args = arguments;
return function(cb) {
args.push( cb );
return fn.apply( null, args );
var thunk_args = [].slice.call( args );
thunk_args.push( cb );
return fn.apply( null, thunk_args );
};
}

Expand All @@ -1784,10 +1785,11 @@ Consider:
```js
function thunkify(fn) {
return function() {
var args = [].slice.call( arguments );
var args = arguments;
return function(cb) {
args.push( cb );
return fn.apply( null, args );
var thunk_args = [].slice.call( args );
thunk_args.push( cb );
return fn.apply( null, thunk_args );
};
};
}
Expand Down

0 comments on commit 594669c

Please sign in to comment.