• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

TypeScript awaiting.delay函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中awaiting.delay函数的典型用法代码示例。如果您正苦于以下问题:TypeScript delay函数的具体用法?TypeScript delay怎么用?TypeScript delay使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了delay函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: Date

export async function retry_until_success<T>(
  opts: RetryUntilSuccess<T>
): Promise<T> {
  if (!opts.start_delay) opts.start_delay = 100;
  if (!opts.max_delay) opts.max_delay = 20000;
  if (!opts.factor) opts.factor = 1.4;

  let next_delay: number = opts.start_delay;
  let tries: number = 0;
  let start_time: number = new Date().valueOf();
  let last_exc: Error | undefined;

  // Return nonempty string if time or tries exceeded.
  function check_done(): string {
    if (
      opts.max_time &&
      next_delay + new Date().valueOf() - start_time > opts.max_time
    ) {
      return "maximum time exceeded";
    }
    if (opts.max_tries && tries >= opts.max_tries) {
      return "maximum tries exceeded";
    }
    return "";
  }

  while (true) {
    try {
      return await opts.f();
    } catch (exc) {
      //console.warn('retry_until_success', exc);
      if (opts.log !== undefined) {
        opts.log("failed ", exc);
      }
      // might try again -- update state...
      tries += 1;
      next_delay = Math.min(opts.max_delay, opts.factor * next_delay);
      // check if too long or too many tries
      let err = check_done();
      if (err) {
        // yep -- game over, throw an error
        let e;
        if (last_exc) {
          e = Error(`${err} -- last error was ${last_exc} -- ${opts.desc}`);
        } else {
          e = Error(`${err} -- ${opts.desc}`);
        }
        //console.warn(e);
        throw e;
      }
      // record exception so can use it later.
      last_exc = exc;

      // wait before trying again
      await awaiting.delay(next_delay);
    }
  }
}
开发者ID:DrXyzzy,项目名称:smc,代码行数:58,代码来源:async-utils.ts


示例2: f

 // Do the i-th burst of writing, then wait.
 async function f(i: number): Promise<void> {
   console.log("chunk", i);
   // put this chunk at the end of the line
   const chunk = content.slice(i * opts.burst, (i + 1) * opts.burst);
   opts.cm.replaceRange(
     opts.cm.getLine(line) + chunk + "\n",
     { line, ch: 0 },
     { line: line + 1, ch: 0 }
   );
   if (opts.cm.getLine(line) !== content.slice(0, (i + 1) * opts.burst)) {
     throw Error("ERROR: corrupted!");
   }
   await delay(opts.delay);
 }
开发者ID:DrXyzzy,项目名称:smc,代码行数:15,代码来源:simulate_typing.ts


示例3: handle_mentions_loop

export async function handle_mentions_loop(
  db: Database,
  poll_interval_s: number = POLL_INTERVAL_S
): Promise<void> {
  while (true) {
    try {
      await handle_all_mentions(db);
    } catch (err) {
      console.warn(`WARNING -- error handling mentions -- ${err}`);
      console.trace();
    }

    await delay(poll_interval_s * 1000);
  }
}
开发者ID:DrXyzzy,项目名称:smc,代码行数:15,代码来源:handle.ts


示例4: function

 term.on("exit", async function() {
   logger.debug("terminal", name, "EXIT -- spawning again");
   const now = new Date().getTime();
   if (now - terminals[name].last_exit <= 15000) {
     // frequent exit; we wait a few seconds, since otherwise
     // restarting could burn all cpu and break everything.
     logger.debug(
       "terminal",
       name,
       "EXIT -- waiting a few seconds before trying again..."
     );
     await delay(3000);
   }
   terminals[name].last_exit = now;
   init_term();
 });
开发者ID:DrXyzzy,项目名称:smc,代码行数:16,代码来源:server.ts


示例5: eventually

export async function eventually(
  f: Function,
  maxtime_ms: number,
  note: string
): Promise<void> {
  const interval = 150;
  for (let i = 0; i < maxtime_ms / interval; i++) {
    try {
      f();
      return;
    } catch (err) {
      await delay(interval);
    }
  }
  throw Error(`timeout -- ${note}`);
}
开发者ID:DrXyzzy,项目名称:smc,代码行数:16,代码来源:util.ts


示例6: function

 opts2.cb = async function(err, resp): Promise<void> {
   opts.cb(err, resp);
   if (!err) {
     read_done = true;
     if (change_queue.length > 0) {
       // CRITICAL: delay, since these must be pushed out in a later event loop.
       // Without this delay, there will be many random failures.
       await delay(0);
       while (change_queue.length > 0) {
         const x = change_queue.shift();
         if (x == null) break; // make typescript happy.
         const { err, change } = x;
         opts.cb(err, change);
       }
     }
   }
   if (!cb_called) {
     cb_called = true;
     cb(err);
   }
 };
开发者ID:DrXyzzy,项目名称:smc,代码行数:21,代码来源:query-function.ts


示例7: set_state

export async function set_state(cm: CodeMirror.Editor, state: State): Promise<void> {
  if (state.ver < VERSION) {
    return; // ignore old version.
  }

  const elt = $(cm.getWrapperElement()).find(".CodeMirror-scroll");
  if (state.pos) {
    elt.css("opacity", 0);
    // We **have to** do the scrollTo in the next render loop, since otherwise
    // the coords below will return the sizing data about
    // the cm instance before the above css font-size change has been rendered.
    // Also, the opacity business avoids some really painful "flicker".
    await delay(0);
    // now in next render loop
    elt.css("opacity", 1);
    cm.scrollTo(0, cm.cursorCoords(state.pos, "local").top);
    cm.refresh();
  }
  if (state.sel) {
    cm.getDoc().setSelections(state.sel);
  }
}
开发者ID:DrXyzzy,项目名称:smc,代码行数:22,代码来源:codemirror-state.ts


示例8: mocha_run

export async function mocha_run(path: string): Promise<void> {
  const w: any = window as any;
  w.mocha.setup("bdd");
  load_mocha_tests(path);
  $(".page-container").css("opacity", 0.3);
  $("#mocha")
    .css({
      border: "1px solid grey",
      "border-radius": "3px",
      "box-shadow": "3px 3px 3px 3px #CCC"
    })
    .focus();
  try {
    await callback(w.mocha.run);
  } catch (failures) {
    console.log("testing - FAIL", failures);
  }
  console.log("testing - complete");
  $(".page-container").css("opacity", 0.1);
  await delay(50);
  $("#mocha").focus();
}
开发者ID:DrXyzzy,项目名称:smc,代码行数:22,代码来源:setup.ts


示例9: emit_connected_in_next_tick

async function emit_connected_in_next_tick(S: SyncTable): Promise<void> {
  await delay(0);
  if (S.get_state() === "connected") {
    S.emit("connected");
  }
}
开发者ID:DrXyzzy,项目名称:smc,代码行数:6,代码来源:global-cache.ts


示例10: test_line

export async function test_line(opts0: TestLineOptions): Promise<void> {
  const opts : TestLineOptions1 = merge(
    {
      length: 48,
      line: 1,
      burst: 10,
      delay: 500,
      wait: 2500
    },
    opts0
  );
  if (opts.length===undefined) opts.length = 48;

  // as any due to this being basically an evil hack.
  if ((opts.cm as any).__test_line) {
    throw Error("already testing this cm!");
  }
  (opts.cm as any).__test_line = true;

  let content: string = "";
  for (let i = 0; i < Math.ceil(opts.length / ALPHA.length); i++) {
    content += ALPHA;
  }
  content = content.slice(0, opts.length);
  const line: number = opts.line - 1;

  // Do the i-th burst of writing, then wait.
  async function f(i: number): Promise<void> {
    console.log("chunk", i);
    // put this chunk at the end of the line
    const chunk = content.slice(i * opts.burst, (i + 1) * opts.burst);
    opts.cm.replaceRange(
      opts.cm.getLine(line) + chunk + "\n",
      { line, ch: 0 },
      { line: line + 1, ch: 0 }
    );
    if (opts.cm.getLine(line) !== content.slice(0, (i + 1) * opts.burst)) {
      throw Error("ERROR: corrupted!");
    }
    await delay(opts.delay);
  }

  // Function that we'll use to verify that the line is as it should be after writing content.
  function verify() {
    console.log("verifying...");
    if (opts.cm.getLine(line) !== content) {
      console.log(`content='${content}'`);
      console.log(`getLine='${opts.cm.getLine(line)}'`);
      throw Error("FAIL -- input was corrupted!");
    }
  }

  // Finally do the test:
  try {
    console.log("do test, starting at ", new Date());

    // Empty the line in prep for testing.
    opts.cm.replaceRange("\n", { line, ch: 0 }, { line: line + 1, ch: 0 });

    // Do the tests, one after the other.  YES, we do want await each before doing the next.
    for (let i = 0; i <= Math.floor(opts.length / opts.burst); i++) {
      await f(i);
    }

    // Did the test work?
    verify();

    // Check again, just in case.
    console.log("wait before verifying second time.");
    await delay(opts.wait);
    verify();

    // No exceptions raised.
    console.log("SUCCESS");
  } catch (err) {
    // Something went wrong.
    console.warn("FAIL -- ", err);
  } finally {
    delete (opts.cm as any).__test_line;
  }
}
开发者ID:DrXyzzy,项目名称:smc,代码行数:81,代码来源:simulate_typing.ts



注:本文中的awaiting.delay函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript awilix.AwilixContainer类代码示例发布时间:2022-05-25
下一篇:
TypeScript awaiting.callback函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap