'use strict';

module.exports = (api) => {

  api.metasync.throttle = (
    // Function throttling
    timeout, // time interval
    fn, // function to be executed once per timeout
    args // arguments array for fn (optional)
  ) => {
    let timer = null;
    let wait = false;
    return function throttled() {
      if (!timer) {
        timer = setTimeout(() => {
          timer = null;
          if (wait) throttled();
        }, timeout);
        if (args) fn(...args);
        else fn();
        wait = false;
      } else {
        wait = true;
      }
    };
  };

  api.metasync.debounce = (
    timeout,
    fn,
    args = []
  ) => {
    let timer;
    return () => {
      clearTimeout(timer);
      timer = setTimeout(() => fn(...args), timeout);
    };
  };

  api.metasync.timeout = (
    // Set timeout for function execution
    timeout, // time interval
    asyncFunction, // async function to be executed
    // done - callback function
    done // callback function on done
  ) => {
    let finished = false;
    done = api.metasync.cb(done);

    const timer = setTimeout(() => {
      if (!finished) {
        finished = true;
        done();
      }
    }, timeout);

    asyncFunction(() => {
      if (!finished) {
        clearTimeout(timer);
        finished = true;
        done();
      }
    });
  };

};

@@ -1150,32 +1150,36 @@
 e;%0A        done(
+null
 );%0A      %7D%0A    %7D
@@ -1210,16 +1210,25 @@
 nction((
+err, data
 ) =%3E %7B%0A 
@@ -1316,16 +1316,25 @@
    done(
+err, data
 );%0A     

Add null as error argument for callback