除非包装,否则可以在功能上调用.catch。

发布于 2025-02-11 16:07:30 字数 1145 浏览 1 评论 0原文

我具有这样的异步函数

router.get("/", async function (req, res, next) {
  const posts = await Post.find({});
  res.render("index", { title: "Homepage", posts });
});

,我可以将其包装在处理捕获量的函数中

function catchAsync(fn) {
  return function (req, res, next) {
    fn(req, res, next).catch((e) => next(e));
  };
}

,因此看起来像这样

router.get(
  "/",
  catchAsync(async function (req, res, next) {
    const posts = await Post.find({});
    res.render("index", { title: "Homepage", posts });
  })
);

,但我的疑问和此帖子的原因是为什么我不能直接在异步函数上拨打。 ,这样吗?

router.get(
  "/",
  async function (req, res, next) {
    const posts = await Post.find({});
    res.render("index", { title: "Homepage", posts });
  }.catch((e) => next(e))
);

我了解我可以做到这一点

router.get("/", async function (req, res, next) {
  my_callback(req, res, next).catch((e) => {
    next(e);
  });
});

let my_callback = async function (req, res, next) {
  const posts = await Post.find({});
  res.render("index", { title: "Homepage", posts });
};

I have an async function such as this

router.get("/", async function (req, res, next) {
  const posts = await Post.find({});
  res.render("index", { title: "Homepage", posts });
});

and I can wrap it in a function that handles its catch

function catchAsync(fn) {
  return function (req, res, next) {
    fn(req, res, next).catch((e) => next(e));
  };
}

so it looks like this

router.get(
  "/",
  catchAsync(async function (req, res, next) {
    const posts = await Post.find({});
    res.render("index", { title: "Homepage", posts });
  })
);

but my doubt, and the reason for this post, is why can't I call .catch directly on the async function, like so?

router.get(
  "/",
  async function (req, res, next) {
    const posts = await Post.find({});
    res.render("index", { title: "Homepage", posts });
  }.catch((e) => next(e))
);

I understand I can do this however

router.get("/", async function (req, res, next) {
  my_callback(req, res, next).catch((e) => {
    next(e);
  });
});

let my_callback = async function (req, res, next) {
  const posts = await Post.find({});
  res.render("index", { title: "Homepage", posts });
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-柠檬树下少年和吉他 2025-02-18 16:07:30

在这里,您是在上调用.catch promise 返回 by 呼叫 my_callback函数

router.get("/", async function (req, res, next) {
  my_callback(req, res, next).catch((e) => {
    next(e);
  });
});

在这里您尝试访问.catch 定义 ,这是语法上是错误的在JavaScript中,

router.get(
  "/",
  async function (req, res, next) {
    const posts = await Post.find({});
    res.render("index", { title: "Homepage", posts });
  }.catch((e) => next(e))
);

如果要包含catch和函数定义,则可以使用尝试...捕获

更多地阅读有关承诺的阅读 - https://deverveper.mozilla.org/en-us/docs/docs/web/javascript/reference/global_obabled/global_objects/promise

Here you are calling .catch on a Promise returned by calling my_callback function

router.get("/", async function (req, res, next) {
  my_callback(req, res, next).catch((e) => {
    next(e);
  });
});

Here you are trying to access .catch on a function definition which is syntactically wrong in JavaScript

router.get(
  "/",
  async function (req, res, next) {
    const posts = await Post.find({});
    res.render("index", { title: "Homepage", posts });
  }.catch((e) => next(e))
);

If you want to include catch with the function definition then you can use try...catch

More reading about promises - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文