问一个关于async 嵌套 catch的问题,这到底有没有catch到呢?

import { deadline } from "https://deno.land/std@0.103.0/async/mod.ts";

async function fakef(x) {
    console.log("x:", x + '\n');
    return await (await fetch("www.ffff.xx")).text()
}

const fakeff = async (x) => await deadline(fakef(x), 1 * 1000);

setInterval(async () => {
    {
        try {
            await fakeff(new Date());
        } catch (error) {
            console.log("catched: ", error)
        }
    }
}, 1000);

从第一条日志来讲,确实catch到了,但是后面又是显示基本一样的一条异常,然后程序就异常退出了? 应该怎么做

x: Fri Aug 06 2021 11:47:18 GMT+0800 (中国标准时间)

catched:  TypeError: Invalid URL
    at deno:core/01_core.js:106:46
    at unwrapOpResult (deno:core/01_core.js:126:13)
    at Object.opSync (deno:core/01_core.js:140:12)
    at new URL (deno:extensions/url/00_url.js:279:26)
    at new Request (deno:extensions/fetch/23_request.js:235:27)
    at deno:extensions/fetch/26_fetch.js:400:29
    at new Promise (<anonymous>)
    at fetch (deno:extensions/fetch/26_fetch.js:387:15)
    at fakef (file:///Users/biluohc/Documents/git/notes/keno/mod.js:10:25)
    at fakeff (file:///Users/biluohc/Documents/git/notes/keno/mod.js:13:44)
error: Uncaught (in promise) TypeError: Invalid URL
    return await (await fetch("www.ffff.xx")).text()
                        ^
    at deno:core/01_core.js:106:46
    at unwrapOpResult (deno:core/01_core.js:126:13)
    at Object.opSync (deno:core/01_core.js:140:12)
    at new URL (deno:extensions/url/00_url.js:279:26)
    at new Request (deno:extensions/fetch/23_request.js:235:27)
    at deno:extensions/fetch/26_fetch.js:400:29
    at new Promise (<anonymous>)
    at fetch (deno:extensions/fetch/26_fetch.js:387:15)
    at fakef (file:///Users/biluohc/Documents/git/notes/keno/mod.js:10:25)
    at fakeff (file:///Users/biluohc/Documents/git/notes/keno/mod.js:13:44)
1赞

没有。

await deadline(fakef(x), 1 * 1000);
               ^^^^^

fakef 的异常在这里丢了。

我发现 deadline(fakef(x), 1 * 1000) 就抓不到同步的错误(比如host RESOLVE, connect失败之类的),

而你给的deadline(await fakef(x), 1 * 1000) deadline效果就没了,

所以实际上try catch 里 await不能同时抓到同步异步的错误吗?还是有什么解决方案

找到原因了,是 deadline 代码中的 bug。我已经提交 PR 了:

fix(async): let async exception of deadline can be caught by justjavac · Pull Request #1105

2赞

@biluohc 已经 merge 了,但是还没有发新版本

新版本已经发布 Release 0.104.0 · denoland/deno_std (github.com)