NodeJS callback plus timeout limit two implementation methods

NodeJS callback timeout limit limited to two implementation methods

The IO operation under NodeJs is asynchronous, sometimes the asynchronous request returns too slow, do not want to wait for the callback? We can add a timeout limit to the callback function. It has not been fails at a certain period of time, and the steps will continue later.

Using Async’s Parallel Implementation

Here I use Async’s Parallel method, Parallel can perform multiple asynchronous callbacks in parallel, normal Next, all the callback functions will be collected into the Parallel final callback function, but there is an exception. If one of the steps have an error throw and directly call the Parallel final callback function, use this feature to implement our functionality .

I encapsulated a function asyncWrapper, and the logic is relatively simple. Direct view code:

   const async = Require ('async'); const asyncwrapper = (fn, interval, ... args) => {let final_callback = args [args.length-1]; async.Parallel ([Function (callback) {args [args.length - 1] = Callback; fn.apply (this, args);}, function (callback) {settimeout (Function () {callback (408);}, interval;}], function (err, resuLTS) {if (err == 408 && results) err = null; final_callback.apply (this, [err] .concat ([results [0]]));});};} {EXPORTS.ASYNCWRAPPER = asyncwrapper;} else {let myfn = (arg_1, arg_2, callback => {settimeout (function () {callback (null, 'value 1:' + arg_1, 'value 2:' + arg_2); }, 1000); ASYNCWRAPPER (Myfn, 2000, 10, 20, (Err, VALUES) => {Console.log (`$ {Err}, $ {VALUES}`);});} 

You can run this JS directly to see the effect:

  Node async-timer.js  

  const asyncWrapper = Require ('./ async-timer.js'). AsyncWrapperConst Fn = (Arg1, Arg2, Callback ) => {// ... assuming that the process is very long, it is possible to time up Callback (null, result_1, result_2);} asyncwrapper (FN, // asynchronous function 10000, // timeout "ar"G1_VALUE ', // Asynchronous Function parameter 1' arg2_value ', // Asynchronous Function parameter 2, there are multiple parameters to continue add (err, results) => {// Results: [Result_1, Result_2] // The final callback, Results is relatively special, and if the fn has multiple returns, RESULTS will return to you in the form of an array});  
The advantage of this solution is Node does not have a lowest version requirement, introducing an Async library can be used, and the defect is that the value returned is presented in the form of an array in a parameter.
Program 2: Implementing

in Promise, add settimeout, and more than time reject. Promise-Timer.js

Const PromiseWrapper = (FN, Interval, … args) => {let final_callback = args [args.length -1]; New promise => {Args [args.length – 1] = (err, … vals) => {if (err) Reject (ERR); Else Resolve (VALS); }; fn.apply (this, args); settimeout (_ => {REICT (‘promise time out’);}, interval);}; (result => {final_callback.apply (this, [null]. Concat (result);}) .catch (err => {final_callback (err);})} f (module.parent) {exports.promiMISEWRAPPER = promiseWrapper;} else {let myfn = (arg_1, arg_2, callback => {settimeout (function) ) {Callback (NULL, ‘VALUE 1:’ + Arg_1, ‘Value 2:’ + arg_2);}, 1000);} promiseWrapper (myfn, 1000, 10, 20, (err, value_1, value_2) => {Console .log (`$ {Err}, Value 1: $ {value_1} … value 2: $ {value_2}`);});}

Module call Method:
   const asyncWrapper = Require ('./ promise-timer.js'). PromisionWrapperConst Fn = (Arg1, Arg2, Callback) => {// .. It is assumed that the process here is long, it is possible to time up Callback (NULL, Result_1, Result_2);} promiseWrapper (FN, // asynchronous function 10000, // timeout 'arg1_value', // asynchronous function parameter 1 'Arg2_Value', / / Parameter 2 of asynchronous functions 2, there are multiple parameters to continue add (Err, Result_1, Result_2) => {// Final callback}); 
This solution requires the NodeJS version higher than 5.0, supporting Promise.

Thanks for reading, I hope to help everyone, thank you for your support of this site!
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment