Skip to content

Commit

Permalink
Allow fn passed to Async.fromNode to be partially applied
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDixon committed May 24, 2020
1 parent 54ab755 commit 6765d2c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Async/Async.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ test('Async fromNode resolution', t => {
Async.fromNode(resCPS)(val).fork(rej(val), res(val))
})

test('Async fromNode partially applied', t => {
t.plan(2)

const val = 'super fun'

const rejCPS = (x, y, cf) => cf(x, y)
const resCPS = (x, y, cf) => cf(null, x, y)

const rej = y => x => t.equal(x, y, 'rejects an erred CPS')
const res = y => x => t.equal(x, y, 'resolves a good CPS')

Async.fromNode(rejCPS)(val)(val).fork(rej(val), res(val))
Async.fromNode(resCPS)(val)(val).fork(rej(val), res(val))
})

test('Async all', t => {
const all = bindFunc(Async.all)

Expand Down
12 changes: 10 additions & 2 deletions src/Async/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,22 @@ function fromNode(fn, ctx) {
throw new TypeError('Async.fromNode: CPS function required')
}

return (...args) =>
Async((reject, resolve) => {
const _fn = curry(fn)

return (...args) => {

if (args.length < _fn.length - 1) {
return fromNode(_fn(...args), ctx)
}

return Async((reject, resolve) => {
fn.apply(ctx,
args.concat(
(err, data) => err ? reject(err) : resolve(data)
)
)
})
}
}

function fromPromise(fn) {
Expand Down

0 comments on commit 6765d2c

Please sign in to comment.