forward methods to asynchronously acquired objects
Forward methods to asynchronously acquired objects.
Specify the method names you want to forward, and provide a callback for getting the object that should receive forwarded method calls.
var assert = require'assert'var forward = require'./'var methods = 'doSomethingAsync'var proxy = forward'double' getSlowDoublerproxydouble4assertequal8 resultconsole.log'ok - async method works'
getSlowDoubler function looks like this, but it could be any sort of
async function, as long as it returns an object with the expected methods:
var service =setTimeoutcallbacknull n * 2100// return the service asynchronously via callbackprocessnextTickcallbacknull service
Any errors that occur while getting the method receiver will be forwarded to the callback:
throw 'whoops'var proxy = forward'myMethod' throwsAnErrorproxymyMethodassertequal'whoops' errmessageconsole.log'ok - got expected error'
The same behaviour will work for errors that are returned via callback or thrown when invoking the method.