如何结合mutliple vue crencurrency错误?

发布于 2025-02-13 04:46:14 字数 2525 浏览 0 评论 0原文

我正在尝试使用 vue并发与我的vue 3/typepript/quasar app。

而且我正在使用 quasar note 在弹出窗口中显示错误消息。

通知弹出窗口是这样触发的:

import { useQuasar } from 'quasar';
const $q = useQuasar();
const triggerNotification = (errorMessage: string) => {
  $q.notify(errorMessage);
};

现在,我需要一种优雅调用triggerNotification时,只要有VUE-汇率任务错误。

我可以通过在每个任务中的每个yart> targe语句的末尾链接catch来做到这一点。

但是我的代码有许多任务,每个任务都有许多屈服语句。链接捕获每个人看起来都很凌乱。

是否有更好的方法可以为每个任务触发triggerNotification函数?

在这里说明问题是一些示例代码:

<script setup lang="ts">
import { useTask, timeout } from 'vue-concurrency';
import { useQuasar } from 'quasar';
const $q = useQuasar();
const triggerNotification = (errorMessage: string) => {
  $q.notify(errorMessage);
};
const exampleAsyncWithError = async () => {
  await timeout(1);
  throw new Error('Ruh oh! Error.');
};
const example1Task = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});
const example2Task = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});
const example3Task = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});
</script>

<template>
  <q-btn label="Run Task" @click="example1Task.perform">
  <q-btn label="Run Task" @click="example2Task.perform">
  <q-btn label="Run Task" @click="example3Task.perform">
</q-btn>
</template>

我还疲倦地实现了usetaskGroup,并在此结束时使用单个catch。但这行不通,因为usetaskGroup不是“当时”。

也许还有另一个解决方案?

I'm trying to use Vue Concurrency with my Vue 3 / TypeScript / Quasar app.

And I am using Quasar Notify to display error messages in a popup.

The notification popups are triggered like this:

import { useQuasar } from 'quasar';
const $q = useQuasar();
const triggerNotification = (errorMessage: string) => {
  $q.notify(errorMessage);
};

And now I need a way to elegantly call triggerNotification whenever there is a vue-concurrency Task error.

I could do it by chaining a catch on the end of every yield statement in every Task.

But my code has many Tasks each with many yield statements. Chaining catch to each one looks messy and verbose.

Is there a better way to trigger the triggerNotification function for every task?

To illustrate the problem here is some example code:

<script setup lang="ts">
import { useTask, timeout } from 'vue-concurrency';
import { useQuasar } from 'quasar';
const $q = useQuasar();
const triggerNotification = (errorMessage: string) => {
  $q.notify(errorMessage);
};
const exampleAsyncWithError = async () => {
  await timeout(1);
  throw new Error('Ruh oh! Error.');
};
const example1Task = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});
const example2Task = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});
const example3Task = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});
</script>

<template>
  <q-btn label="Run Task" @click="example1Task.perform">
  <q-btn label="Run Task" @click="example2Task.perform">
  <q-btn label="Run Task" @click="example3Task.perform">
</q-btn>
</template>

I also tired implementing a useTaskGroup and with a single catch on the end of that. But it doesn't work because useTaskGroup is not "thenable".

Maybe there is another solution?

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

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

发布评论

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

评论(1

清泪尽 2025-02-20 04:46:14

以下文本从此在github上非常有用的答案 VUE并发的创建者:

我认为这取决于您要如何处理错误。我有时也会使用这些弹出通知 - 当没有相关位置显示错误的情况下,尤其是在后台中有些事情中断时,它们适用于情况。

因此,我的工作流确实是90%次 - 显示错误“适当”,并使用task.last.error。在剩下的情况下,我会在任务内尝试 ,并使用triggerNotification之类的东西。

BUF如果您打算大量使用triggernotification,那么尝试这么多的捕获量确实不是最佳选择。

您能做的就是将整个任务执行的操作中包裹起来。

<script setup lang="ts">
import { useTask, timeout } from 'vue-concurrency';
import { useQuasar } from 'quasar';
const $q = useQuasar();
const throwError = async () => {
  await timeout(1);
  throw new Error('Ruh oh! Error.');
};
const exampleTask = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});

const save = async () => {
  try {
   await exampleTask.perform();
  } catch (e) {
    $q.notify(error.message);
}
};
</script>

<template>
  <q-btn label="Run Task" @click="save"></q-btn>
</template>

您将经常创建这些包装器功能,因此您可以为其编写一些更高的订单功能:

const performAndNotify = async (task, ...args) => {
  try {
  await task.perform(...args);
 } catch (error) {
  $q.notify(error.message);
 }
};

然后,您可以在多个位置重复使用此功能。

The below text is copied from this very helpful answer on Github, made by the creator of Vue Concurrency:

I think this depends on how you want to handle the errors. I sometimes use these popup notifications as well - they're good for cases when there's no relevant place to display the error, and especially if something breaks in the background.

So my workflow usally is 90% times - display error "in place" and use task.last.error. In the rest cases I do try catch inside the task and use something like your triggerNotification.

Buf if you plan to use triggerNotification a lot, it's indeed maybe not optimal to do so many try catch.

What you can do is wrap the entire task perform in a try catch.

<script setup lang="ts">
import { useTask, timeout } from 'vue-concurrency';
import { useQuasar } from 'quasar';
const $q = useQuasar();
const throwError = async () => {
  await timeout(1);
  throw new Error('Ruh oh! Error.');
};
const exampleTask = useTask(function* () {
  yield throwError().catch((err) => {
    triggerNotification(err.message);
  });
});

const save = async () => {
  try {
   await exampleTask.perform();
  } catch (e) {
    $q.notify(error.message);
}
};
</script>

<template>
  <q-btn label="Run Task" @click="save"></q-btn>
</template>

You'd be creating these wrapper functions a lot, so you can write some higher order function for it:

const performAndNotify = async (task, ...args) => {
  try {
  await task.perform(...args);
 } catch (error) {
  $q.notify(error.message);
 }
};

Then you can reuse this function at multiple places.

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