Skip to content

Commit 765a4fd

Browse files
author
tunnckoCore
committed
feat(options): allow passing options object
directly passed to try-catch-core, add more tests for guarantees
1 parent 2420002 commit 765a4fd

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ var utils = require('./utils')
3939
* @api public
4040
*/
4141

42-
module.exports = function alwaysDone (fn, done) {
42+
module.exports = function alwaysDone (fn, opts, done) {
43+
if (typeof opts === 'function') {
44+
done = opts
45+
opts = null
46+
}
4347
if (typeof done === 'function') {
44-
utils.tryCatchCore(fn, function cb (err, val) {
48+
utils.tryCatchCore.call(this, fn, opts, function cb (err, val) {
4549
// handle errors
4650
if (err) {
4751
done(err)
@@ -64,9 +68,7 @@ module.exports = function alwaysDone (fn, done) {
6468

6569
// handle observables
6670
if (val && typeof val.subscribe === 'function') {
67-
utils.tryCatchCore(function (cb) {
68-
utils.subscribe(val, cb)
69-
}, done)
71+
utils.subscribe(val, done)
7072
return
7173
}
7274

@@ -83,7 +85,7 @@ module.exports = function alwaysDone (fn, done) {
8385
}
8486

8587
return function thunk (cb) {
86-
return alwaysDone(fn, cb)
88+
return alwaysDone.call(this, fn, opts, cb)
8789
}
8890
}
8991

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"lazy-cache": "^2.0.1",
2929
"on-stream-end": "^1.0.0",
3030
"stream-exhaust": "^1.0.1",
31-
"try-catch-core": "^1.0.3"
31+
"try-catch-core": "^2.0.1"
3232
},
3333
"devDependencies": {
3434
"assertit": "^0.1.0",

test.js

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ require('./test/promises')
2020
require('./test/streams')
2121

2222
require('./test/sync')
23+
24+
require('./test/options')

test/options.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*!
2+
* always-done <https://github.com/hybridables/always-done>
3+
*
4+
* Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
5+
* Released under the MIT license.
6+
*/
7+
8+
/* jshint asi:true */
9+
10+
'use strict'
11+
12+
var fs = require('fs')
13+
var test = require('assertit')
14+
var path = require('path')
15+
var alwaysDone = require('../index')
16+
17+
test('should allow passing custom context through options', function (done) {
18+
alwaysDone(function () {
19+
test.strictEqual(this.num, 123)
20+
test.strictEqual(this.bool, true)
21+
test.strictEqual(arguments.length, 0)
22+
}, {
23+
context: { num: 123, bool: true }
24+
}, done)
25+
})
26+
27+
test('should allow passing custom context through `.call`', function (done) {
28+
alwaysDone.call({
29+
foo: 'bar'
30+
}, function () {
31+
test.strictEqual(this.foo, 'bar')
32+
test.strictEqual(arguments.length, 0)
33+
}, done)
34+
})
35+
36+
test('should allow passing custom arguments through options', function (done) {
37+
var filepath = path.join(__dirname, '../package.json')
38+
alwaysDone(fs.readFile, {
39+
args: [filepath, 'utf8']
40+
}, function (err, res) {
41+
test.strictEqual(err, null)
42+
test.strictEqual(typeof res, 'string')
43+
44+
var json = JSON.parse(res)
45+
test.strictEqual(json.license, 'MIT')
46+
done()
47+
})
48+
})
49+
50+
test('should pass callback arg, when `fn` is async or when sync but passCallback: true', function (done) {
51+
alwaysDone(function (foo, bar) {
52+
test.strictEqual(arguments.length, 2)
53+
test.strictEqual(foo, 'bar')
54+
test.strictEqual(bar, 'qux')
55+
}, {
56+
args: ['bar', 'qux']
57+
}, function (err) {
58+
test.strictEqual(err, null)
59+
alwaysDone(function (aaa, bbb) {
60+
test.strictEqual(arguments.length, 3)
61+
test.strictEqual(aaa, 111)
62+
test.strictEqual(bbb, 222)
63+
64+
// get the callback (3rd argument)
65+
// intentionally getting it in this way
66+
var cb = arguments[2]
67+
cb()
68+
}, {
69+
passCallback: true,
70+
args: [111, 222]
71+
}, done)
72+
})
73+
})

0 commit comments

Comments
 (0)