@polkadot/types/types#AnyFunction TypeScript Examples

The following examples show how to use @polkadot/types/types#AnyFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From bodhi.js with Apache License 2.0 7 votes vote down vote up
runWithRetries = async <F extends AnyFunction>(
  fn: F,
  args: any[] = [],
  maxRetries: number = 200,
  interval: number = 100
): Promise<F extends (...args: any[]) => infer R ? R : never> => {
  let res;
  let tries = 0;

  while (!res && tries++ < maxRetries) {
    try {
      res = await fn(...args);
    } catch (e) {
      if (tries === maxRetries) throw e;
    }

    if ((tries === 1 || tries % 10 === 0) && !res) {
      console.log(`<local mode runWithRetries> still waiting for result # ${tries}/${maxRetries}`);
    }

    await sleep(interval);
  }

  return res;
}
Example #2
Source File: utils.ts    From bodhi.js with Apache License 2.0 7 votes vote down vote up
runWithTiming = async <F extends AnyFunction>(
  fn: F,
  repeats: number = 3
): Promise<{
  time: number;
  res: F extends (...args: any[]) => infer R ? R | string : any;
}> => {
  let res = null;
  const t0 = performance.now();
  let runningErr = false;
  let timedout = false;

  try {
    for (let i = 0; i < repeats; i++) {
      res = await Promise.race([fn(), sleep(TIME_OUT)]);

      // fn should always return something
      if (res === null) {
        res = `error in runWithTiming: timeout after ${TIME_OUT / 1000} seconds`;
        timedout = true;
        break;
      }
    }
  } catch (e) {
    res = `error in runWithTiming: ${(e as any).toString()}`;
    runningErr = true;
  }

  const t1 = performance.now();
  const time = runningErr ? -1 : timedout ? -999 : (t1 - t0) / repeats;

  return {
    res,
    time
  };
}