diff --git a/index.js b/index.js index 1e13b19..1560a0b 100644 --- a/index.js +++ b/index.js @@ -39,9 +39,13 @@ var utils = require('./utils') * @api public */ -module.exports = function alwaysDone (fn, done) { +module.exports = function alwaysDone (fn, opts, done) { + if (typeof opts === 'function') { + done = opts + opts = null + } if (typeof done === 'function') { - utils.tryCatchCore(fn, function cb (err, val) { + utils.tryCatchCore.call(this, fn, opts, function cb (err, val) { // handle errors if (err) { done(err) @@ -64,9 +68,7 @@ module.exports = function alwaysDone (fn, done) { // handle observables if (val && typeof val.subscribe === 'function') { - utils.tryCatchCore(function (cb) { - utils.subscribe(val, cb) - }, done) + utils.subscribe(val, done) return } @@ -83,7 +85,7 @@ module.exports = function alwaysDone (fn, done) { } return function thunk (cb) { - return alwaysDone(fn, cb) + return alwaysDone.call(this, fn, opts, cb) } } diff --git a/package.json b/package.json index a88410a..7000be3 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "lazy-cache": "^2.0.1", "on-stream-end": "^1.0.0", "stream-exhaust": "^1.0.1", - "try-catch-core": "^1.0.3" + "try-catch-core": "^2.0.1" }, "devDependencies": { "assertit": "^0.1.0", diff --git a/test.js b/test.js index eb8ff9d..9d54c8e 100644 --- a/test.js +++ b/test.js @@ -20,3 +20,5 @@ require('./test/promises') require('./test/streams') require('./test/sync') + +require('./test/options') diff --git a/test/options.js b/test/options.js new file mode 100644 index 0000000..de2e1fb --- /dev/null +++ b/test/options.js @@ -0,0 +1,73 @@ +/*! + * always-done + * + * Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) + * Released under the MIT license. + */ + +/* jshint asi:true */ + +'use strict' + +var fs = require('fs') +var test = require('assertit') +var path = require('path') +var alwaysDone = require('../index') + +test('should allow passing custom context through options', function (done) { + alwaysDone(function () { + test.strictEqual(this.num, 123) + test.strictEqual(this.bool, true) + test.strictEqual(arguments.length, 0) + }, { + context: { num: 123, bool: true } + }, done) +}) + +test('should allow passing custom context through `.call`', function (done) { + alwaysDone.call({ + foo: 'bar' + }, function () { + test.strictEqual(this.foo, 'bar') + test.strictEqual(arguments.length, 0) + }, done) +}) + +test('should allow passing custom arguments through options', function (done) { + var filepath = path.join(__dirname, '../package.json') + alwaysDone(fs.readFile, { + args: [filepath, 'utf8'] + }, function (err, res) { + test.strictEqual(err, null) + test.strictEqual(typeof res, 'string') + + var json = JSON.parse(res) + test.strictEqual(json.license, 'MIT') + done() + }) +}) + +test('should pass callback arg, when `fn` is async or when sync but passCallback: true', function (done) { + alwaysDone(function (foo, bar) { + test.strictEqual(arguments.length, 2) + test.strictEqual(foo, 'bar') + test.strictEqual(bar, 'qux') + }, { + args: ['bar', 'qux'] + }, function (err) { + test.strictEqual(err, null) + alwaysDone(function (aaa, bbb) { + test.strictEqual(arguments.length, 3) + test.strictEqual(aaa, 111) + test.strictEqual(bbb, 222) + + // get the callback (3rd argument) + // intentionally getting it in this way + var cb = arguments[2] + cb() + }, { + passCallback: true, + args: [111, 222] + }, done) + }) +})