Implementing timeouts for node.js callbacks

前端 未结 4 582
执念已碎
执念已碎 2020-12-25 11:07

This is a typical situation in node.js:

asyncFunction(arguments, callback);

When asynFunction completes, callback

4条回答
  •  清酒与你
    2020-12-25 11:58

    You probably need to come out with a solution of your own. Like

    function callBackWithATimeout (callback, timeout) {
      var run, timer;
      run = function () {
        if (timer) {
          clearTimeout(timer);
          timer = null;
          callback.apply(this, arguments);
        }
      };
      timer = setTimeout(run, timeout, "timeout");
      return run;
    }
    

    and then

    asyncFunction(arguments, callBackWithATimeout(callback, 2000));
    

提交回复
热议问题