跳至主内容
版本:11.x

中断过程调用

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

tRPC支持通过标准的AbortController/AbortSignal API中断过程调用。你只需将AbortSignal传递给查询或变更选项,并在需要取消请求时调用AbortController实例的abort方法即可。

utils.ts
ts
import { createTRPCClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from './server';
 
const client = createTRPCClient<AppRouter>({
links: [
httpBatchLink({
url: 'http://localhost:3000/trpc',
}),
],
});
 
// 1. Create an AbortController instance - this is a standard javascript API
const ac = new AbortController();
 
// 2. Pass the signal to a query or mutation
const query = client.userById.query('id_bilbo', { signal: ac.signal });
 
// 3. Cancel the request if needed
ac.abort();
utils.ts
ts
import { createTRPCClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from './server';
 
const client = createTRPCClient<AppRouter>({
links: [
httpBatchLink({
url: 'http://localhost:3000/trpc',
}),
],
});
 
// 1. Create an AbortController instance - this is a standard javascript API
const ac = new AbortController();
 
// 2. Pass the signal to a query or mutation
const query = client.userById.query('id_bilbo', { signal: ac.signal });
 
// 3. Cancel the request if needed
ac.abort();