2

随着ES2022 接受了顶级等待,我想知道假设它await import("./path/to/module")根本没有超时是否可以保存。这是我想做的:

// src/commands/do-a.mjs

console.log("Doing a...");
await doSomethingThatTakesHours();
console.log("Done.");
// src/commands/do-b.mjs

console.log("Doing b...");
await doSomethingElseThatTakesDays();
console.log("Done.");
// src/commands/do-everything.mjs

await import("./do-a");
await import("./do-b");

这是我在运行时期望看到的node src/commands/do-everything.mjs

Doing a...
Done.
Doing b...
Done.

我找不到任何关于顶级等待超时的提及,但我想知道我想要做的是否是对该功能的滥用。理论上,Node.js(或 Deno)可能会在达到某个预定义的时间上限(例如 30 秒)后抛出异常。

以下是我在 TLA 之前处理相同任务的方式:

// src/commands/do-a.cjs
import { autoStartCommandIfNeeded } from "@kachkaev/commands";

const doA = async () => {
  console.log("Doing a...");
  await doSomethingThatTakesHours();
  console.log("Done.");
}

export default doA;

autoStartCommandIfNeeded(doA, __filename);
// src/commands/do-b.cjs
import { autoStartCommandIfNeeded } from "@kachkaev/commands";

const doB = async () => {
  console.log("Doing b...");
  await doSomethingThatTakesDays();
  console.log("Done.");
}

export default doB;

autoStartCommandIfNeeded(doB, __filename);
// src/commands/do-everything.cjs
import { autoStartCommandIfNeeded } from "@kachkaev/commands";
import doA from "./do-a";
import doB from "./do-b";

const doEverything = () => {
  await doA();
  await doB();
}

export default doEverything;

autoStartCommandIfNeeded(doEverything, __filename);

autoStartCommandIfNeeded()__filename如果匹配则执行函数require.main?.filename

4

2 回答 2

1

据我所知,async-await 默认没有超时。例如,有一个 await-timeout 包,它正在添加超时行为。例子:

import Timeout from 'await-timeout';
 
const timer = new Timeout();
try {
  await Promise.race([
    fetch('https://example.com'),
    timer.set(1000, 'Timeout!')
  ]);
} finally {
  timer.clear();
}

取自文档:https ://www.npmjs.com/package/await-timeout

如您所见, aTimeout被实例化,它的set方法定义了超时和超时消息。

于 2021-08-07T17:46:32.053 回答
1

答:不,await 没有顶级超时。

此功能实际上在 Deno 中用于 Web 服务器,例如:

import { serve } from "https://deno.land/std@0.103.0/http/server.ts";

const server = serve({ port: 8080 });
console.log(`HTTP webserver running.  Access it at:  http://localhost:8080/`);

console.log("A");

for await (const request of server) {
  let bodyContent = "Your user-agent is:\n\n";
  bodyContent += request.headers.get("user-agent") || "Unknown";

  request.respond({ status: 200, body: bodyContent });
}

console.log("B");

在这个例子中,“A”被打印在控制台中,“B”直到网络服务器关闭(这不会自动发生)。

于 2021-08-07T23:52:53.583 回答