diff options
author | Alex Early <alexander.early@gmail.com> | 2016-04-07 13:43:29 -0700 |
---|---|---|
committer | Alex Early <alexander.early@gmail.com> | 2016-04-07 13:43:29 -0700 |
commit | 0a3c1b14654ae8b58839cefdad1e8aa8cf147631 (patch) | |
tree | 2bad52cc3ee840fc0ad86bfa4afb8db3438e4361 | |
parent | e979953a11f66886f6d387a9e3391917b115127e (diff) | |
parent | 27e0a8bf7934d5f1a9ab7ddf3afef9bec33c54ef (diff) | |
download | async-0a3c1b14654ae8b58839cefdad1e8aa8cf147631.tar.gz |
Merge pull request #1094 from bspates/bind-after-ensure-async
Bind Context to Functions wrapped by initialParams
-rw-r--r-- | lib/internal/initialParams.js | 2 | ||||
-rw-r--r-- | mocha_test/ensureAsync.js | 32 |
2 files changed, 33 insertions, 1 deletions
diff --git a/lib/internal/initialParams.js b/lib/internal/initialParams.js index 635caba..55c4e12 100644 --- a/lib/internal/initialParams.js +++ b/lib/internal/initialParams.js @@ -3,6 +3,6 @@ import rest from 'lodash/rest'; export default function (fn) { return rest(function (args/*..., callback*/) { var callback = args.pop(); - fn(args, callback); + fn.call(this, args, callback); }); } diff --git a/mocha_test/ensureAsync.js b/mocha_test/ensureAsync.js new file mode 100644 index 0000000..c33e344 --- /dev/null +++ b/mocha_test/ensureAsync.js @@ -0,0 +1,32 @@ +var async = require('../lib');
+var expect = require('chai').expect;
+
+describe('ensureAsync', function() {
+ var passContext = function(cb) {
+ cb(this);
+ };
+
+ it('should propely bind context to the wrapped function', function(done) {
+
+ // call bind after wrapping with ensureAsync
+ var context = {context: "post"};
+ var postBind = async.ensureAsync(passContext);
+ postBind = postBind.bind(context);
+ postBind(function(ref) {
+ expect(ref).to.equal(context);
+ done();
+ });
+ });
+
+ it('should not override the bound context of a function when wrapping', function(done) {
+
+ // call bind before wrapping with ensureAsync
+ var context = {context: "pre"};
+ var preBind = passContext.bind(context);
+ preBind = async.ensureAsync(preBind);
+ preBind(function(ref) {
+ expect(ref).to.equal(context);
+ done();
+ });
+ });
+});
|