You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Could you please provide some examples how to use async.timeout? I can't understand how to pass parameters and non-timeout errors to the wrapped function.
The text was updated successfully, but these errors were encountered:
The wrapped function returned by async.timeout is invoked the same way as the asyncFn you pass in, assuming it follow the node convention of having a callback as the last argument. The only difference is that if it takes longer than the milliseconds passed in when wrapping it, the callback will be passed an error with the code ETIMEDOUT.
For example, if this is the function you want to wrap:
// your async function you want to timeout after a certain amount of time, e.g. 500 msfunctionapiMethod(req,callback){api.getData(req,function(err,data){// pass any errors the same way you would normallyif(err)returncallback(err);// do your processing on data// pass results to the callback as you normally wouldreturncallback(null,data);)}
and you normally call apiMethod:
apiMethod({foo: 'foo'},function(err,data){if(err)returnhandleError(err);// your error handler // the rest of your code});
Then to wrap it in async.timeout, you would just do:
varwrappedApiMethod=async.timeout(apiMethod,500);// now you can invoke it exactly the same way as apiMethodwrappedApiMethod({foo: 'foo'},function(err,data){// if the method takes more than 500 ms to execute err will have the// code `ETIMEDOUT`if(err)returnhandleError(err);// the rest of your code});
I'll update the timeout docs to hopefully make this a little more clear.
Could you please provide some examples how to use
async.timeout
? I can't understand how to pass parameters and non-timeout errors to thewrapped
function.The text was updated successfully, but these errors were encountered: