async.apply doesn't work as documented. #129

jcayzac opened this Issue May 10, 2012 · 4 comments


None yet

3 participants


Despite what the documentation implies, it cannot be used with

var fs = require('fs');
var async = require('async');
var cb = function() { console.log(JSON.stringify(arguments, null, 2)) };
var f = async.apply(fs.readFile, 'hello.txt', 'utf-8');

async.parallel([f], cb)
  "1": [
    "Hello, World!\n"
}{a:f}, cb)
...cb never gets called...

Same result with f = fs.readFile.bind(null, 'hello.txt', 'utf-8').


Apparently this is because f gets re-bound somewhere by to some array of functions. If I log this inside f, I get [ [Function] ].


This seems to be a serious problem. @caolan have you seen this?

I agree with @jcayzac that it seems to be an issue with auto, not apply. I'll investigate further


I figured out the issue. When calls the continuation created by async.apply, it includes an extra 'results' argument (the results of the previous functions) as expected, which is passed on to the original function passed to apply.

So when we create a continuation with

var f = async.apply(fs.readFile, 'hello.txt', 'utf-8');

And then it is called by like

f(callback, {});

The result is as if we called

fs.readFile('hello.txt', 'utf-8', callback, {});

Which happens never to call the callback, as you can test for yourselves. Whether or not this design decision on the part of the authors of fs is something we agree with, this phenomenon may reappear in any number of places, the crux of the issue being that it isn't necessarily safe to pass extra arguments around to arbitrary functions. Therefore, in order to use the functionality of safely, all functions MUST be manually wrapped in a way that prevents the results argument from being passed around unintentionally.

I propose adding something to this effect to the documentation of


yep optional arguments cause all kinds of issues with partial application :( ...I think adding a warning to the readme is the best we can do.

@caolan caolan closed this Mar 2, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment