Skip to content

Instantly share code, notes, and snippets.

@jpillora
Last active December 22, 2023 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jpillora/6152557 to your computer and use it in GitHub Desktop.
Save jpillora/6152557 to your computer and use it in GitHub Desktop.
Join API
"use strict"
api1 =
a: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+1), 500
b: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+2), 500
api2 =
b: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+3), 500
c: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+4), 500
join = ->
newapi = {}
for api in arguments
for key, val of api
#already exists
if newapi[key]
newapi[key].fns.push val
continue
#close around key and api, then init
((key, api) ->
newapi[key] = ->
args = Array::slice.call arguments
callback = args.pop()
asyncs = []
for fn in newapi[key].fns
asyncs.push async.apply.apply null, [fn].concat args
async.parallel asyncs, callback
null
newapi[key].fns = [val]
)(key,api)
newapi
all = join(api1, api2)
all.a 3,3,3,(err, results) ->
results
# [ 11, 12 ]
api1 =
a: -> 1
b: -> 2
api2 =
b: -> 3
c: -> 4
join = ->
result = {}
for api in arguments
for key, val of api
#already exists
if result[key]
result[key].fns.push val
continue
#close around key and api, then init
((key, api) ->
result[key] = ->
rets = []
for fn in result[key].fns
rets.push fn.apply null, arguments
rets
result[key].fns = [val]
)(key,api)
result
joint = join(api1, api2)
json joint.b() # [2,3]
@williamsumne98
Copy link

nogauridan

@williamsumne98
Copy link

how to add nogaurdian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment