async icon indicating copy to clipboard operation
async copied to clipboard

[DOC] async.compose

Open matteospampani opened this issue 12 years ago • 2 comments

In the documentation provided there is written:

compose(fn1, fn2...) Creates a function which is a composition of the passed asynchronous functions. Each function consumes the return value of the function that follows. Composing functions f(), g(), and h() would produce the result of f(g(h())), only this version uses callbacks to obtain the return values.

Let's suppose this call: compose( f, g, h ); In my opinion the output should be h(g(f())) because the first function executed is f() then g() is executed on the f() result [ = g(f()) ] and recursively out = h(g(f())).

Do you agree with me or am I missing something? Thank you for this nice work!

matteospampani avatar May 06 '14 13:05 matteospampani

I help you bring the source code here:

async.compose = function (/* functions... */) {
      return async.seq.apply(null, Array.prototype.reverse.call(arguments));
    };

async.seq = function (/* functions... */) {
        var fns = arguments;
        return function () {
            var that = this;
            var args = Array.prototype.slice.call(arguments);
            var callback = args.pop();
            async.reduce(fns, args, function (newargs, fn, cb) {
                fn.apply(that, newargs.concat([function () {
                    var err = arguments[0];
                    var nextargs = Array.prototype.slice.call(arguments, 1);
                    cb(err, nextargs);
                }]))
            },
            function (err, results) {
                callback.apply(that, [err].concat(results));
            });
        };
    };

fritx avatar May 06 '14 13:05 fritx

Ok thank you! I didn't noticed it before...

matteospampani avatar May 06 '14 13:05 matteospampani