Permalink
Browse files

switched to using toAsync method

  • Loading branch information...
1 parent 4586247 commit d9551afee82394df788c7a3fde742d5ece1fdc4b @seanhess seanhess committed Jun 20, 2012
Showing with 43 additions and 44 deletions.
  1. +10 −17 index.coffee
  2. +22 −22 index.js
  3. +11 −5 test.coffee
View
@@ -50,25 +50,12 @@ fjs = (_) ->
# also allows you to CREATE a series
makeSeries = (funcs...) ->
index = 0
-
- callAsync = (func, args, cb) ->
- func args..., (err, args...) ->
- if err? then return cb err
- nextInSeries args..., cb
-
- callSync = (func, args, cb) ->
- result = func args...
- nextInSeries result, cb
-
nextInSeries = (args..., cb) ->
func = funcs[index++]
- # console.log "CALLING", func?, args
if not func? then return cb null, args...
-
- if func.length is args.length
- callSync func, args, cb
- else
- callAsync func, args, cb
+ func args..., (err, args...) ->
+ if err? then return cb err
+ nextInSeries args..., cb
# make a series and call it immediately
# TODO is there a way to automatically detect this, like curry does?
@@ -77,7 +64,13 @@ fjs = (_) ->
seriesChain = makeSeries funcs...
seriesChain cb
- async = {series, makeSeries}
+ toAsync = (f) ->
+ (args..., cb) ->
+ process.nextTick ->
+ res = f args...
+ cb null, res
+
+ async = {series, makeSeries, toAsync}
## OBJECTS
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -57,7 +57,7 @@ describe 'fjs', ->
getData = () -> "bob"
getDetails = (id) -> {id: id, message: "hi " + id}
- fjs.series getData, getDetails, (err, data) ->
+ fjs.series fjs.toAsync(getData), fjs.toAsync(getDetails), (err, data) ->
assert.ifError err
assert.ok data
assert.equal data.id, "bob"
@@ -73,15 +73,21 @@ describe 'fjs', ->
assert.ok !data
done()
- it 'should work with curry', ->
- asyncAdd = curry (n, num, cb) -> cb null, num+n
- mult = curry (n, num) -> num*n
- myMethod = fjs.series asyncAdd(2), mult(2), (err, num) ->
+ it 'should work with curry', (done) ->
+
+ asyncAdd = curry (n, num, cb) ->
+ cb null, num+n
+
+ mult = curry (n, num) ->
+ num*n
+
+ fjs.series asyncAdd(2, 2), fjs.toAsync(mult(2)), (err, num) ->
assert.ifError err
assert.equal num, 8
done()
it 'should behave like compose and let you make one for use later', (done) ->
+ console.log "NEXT"
getData = (cb) -> process.nextTick -> cb null, "bob"
getDetails = (id, cb) -> process.nextTick -> cb null, {id: id, message: "hi " + id}

0 comments on commit d9551af

Please sign in to comment.