如何结合mutliple vue crencurrency错误?
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下文本从此在github上非常有用的答案 VUE并发的创建者:
我认为这取决于您要如何处理错误。我有时也会使用这些弹出通知 - 当没有相关位置显示错误的情况下,尤其是在后台中有些事情中断时,它们适用于情况。
因此,我的工作流确实是90%次 - 显示错误“适当”,并使用
task.last.error
。在剩下的情况下,我会在任务内尝试 ,并使用triggerNotification
之类的东西。BUF如果您打算大量使用triggernotification,那么尝试这么多的捕获量确实不是最佳选择。
您能做的就是将整个任务执行的操作中包裹起来。
您将经常创建这些包装器功能,因此您可以为其编写一些更高的订单功能:
然后,您可以在多个位置重复使用此功能。
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 dotry catch
inside the task and use something like yourtriggerNotification
.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.
You'd be creating these wrapper functions a lot, so you can write some higher order function for it:
Then you can reuse this function at multiple places.