本文整理汇总了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);
}
}
}
示例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);
}
示例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);
}
}
示例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();
});
示例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}`);
}
示例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);
}
};
示例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);
}
}
示例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();
}
示例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");
}
}
示例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;
}
}