Skip to content

Commit de54156

Browse files
committed
Allow fn passed to Async.fromNode to be partially applied
1 parent e5e779a commit de54156

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Async/Async.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ test('Async fromNode resolution', t => {
225225
Async.fromNode(resCPS)(val).fork(rej(val), res(val))
226226
})
227227

228+
test('Async fromNode partially applied', t => {
229+
t.plan(2)
230+
231+
const val = 'super fun'
232+
233+
const rejCPS = (x, y, cf) => cf(x, y)
234+
const resCPS = (x, y, cf) => cf(null, x, y)
235+
236+
const rej = y => x => t.equal(x, y, 'rejects an erred CPS')
237+
const res = y => x => t.equal(x, y, 'resolves a good CPS')
238+
239+
Async.fromNode(rejCPS)(val)(val).fork(rej(val), res(val))
240+
Async.fromNode(resCPS)(val)(val).fork(rej(val), res(val))
241+
})
242+
228243
test('Async all', t => {
229244
const all = bindFunc(Async.all)
230245

src/Async/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,22 @@ function fromNode(fn, ctx) {
4848
throw new TypeError('Async.fromNode: CPS function required')
4949
}
5050

51-
return (...args) =>
52-
Async((reject, resolve) => {
51+
const _fn = curry(fn)
52+
53+
return (...args) => {
54+
55+
if (args.length < _fn.length - 1) {
56+
return fromNode(_fn(...args), ctx)
57+
}
58+
59+
return Async((reject, resolve) => {
5360
fn.apply(ctx,
5461
args.concat(
5562
(err, data) => err ? reject(err) : resolve(data)
5663
)
5764
)
5865
})
66+
}
5967
}
6068

6169
function fromPromise(fn) {

0 commit comments

Comments
 (0)