From ff06a8283458db5624447f99d2c359e1711b8c99 Mon Sep 17 00:00:00 2001 From: Augusto Franzoia Date: Tue, 8 Mar 2016 01:21:24 -0300 Subject: [PATCH] Support dynamic arguments in async.constant (ref. #1016) --- lib/constant.js | 5 +++-- mocha_test/constant.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 mocha_test/constant.js diff --git a/lib/constant.js b/lib/constant.js index 706507f30..ecaff802f 100644 --- a/lib/constant.js +++ b/lib/constant.js @@ -4,7 +4,8 @@ import rest from 'lodash/rest'; export default rest(function(values) { var args = [null].concat(values); - return function (cb) { - return cb.apply(this, args); + return function () { + var callback = [].slice.call(arguments).pop(); + return callback.apply(this, args); }; }); diff --git a/mocha_test/constant.js b/mocha_test/constant.js new file mode 100644 index 000000000..d76076c15 --- /dev/null +++ b/mocha_test/constant.js @@ -0,0 +1,28 @@ +var async = require('../lib'); +var expect = require('chai').expect; + +describe('constant', function () { + + it('basic usage', function(done){ + var f = async.constant(42, 1, 2, 3); + f(function (err, value, a, b, c) { + expect(err).to.equal(null); + expect(value).to.equal(42); + expect(a).to.equal(1); + expect(b).to.equal(2); + expect(c).to.equal(3); + done(); + }); + }); + + it('called with multiple arguments', function(done){ + var f = async.constant(42, 1, 2, 3); + f('argument to ignore', 'another argument', function (err, value, a) { + expect(err).to.equal(null); + expect(value).to.equal(42); + expect(a).to.equal(1); + done(); + }); + }); + +});