Skip to content

constant() should support dynamic args #1016

@ross-nordstrom

Description

@ross-nordstrom

In v1.5.2 constant seems to be hard-coded to be invoked with 1 arg: a callback. This is limiting

    async.constant = _restParam(function(values) {
        var args = [null].concat(values);
        return function (callback) {
            return callback.apply(this, args);
        };
    });

I'd like to propose the inner, curried function take dynamic args as well:

    async.constant = _restParam(function(values) {
        var args = [null].concat(values);
        return _restParam(function (callbackArgs) { // <= We only care about the callback,
            var callback = callbackArgs.pop();      //    and assume it's the last arg
            return callback.apply(this, args);      //    when the curried function is invoked
        });
    });

Why do I care?

Composability. I don't want to care about the callback-signature of other functions in Async-chains.

async.waterfall([
  doSomething/*(cb)*/, //=> Invokes cb with (err, arg1, arg2)
  async.constant('Pretend doSomething did somethingElse')/*(arg1, arg2, cb)*/
], doneCb);

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions