跳至主要內容
版本:11.x

中止程序呼叫

tRPC 遵循中止程序的行業標準。您只需將 AbortSignal 傳遞給查詢或變更選項,並在需要取消請求時調用 AbortController 實例的 abort 方法。

utils.ts
ts
// @filename: server.ts
import { createTRPCClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from 'server.ts';
 
const proxy = createTRPCClient<AppRouter>({
links: [
httpBatchLink({
url: 'https://#: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 = proxy.userById.query('id_bilbo', { signal: ac.signal });
 
// 3. Cancel the request if needed
ac.abort();
utils.ts
ts
// @filename: server.ts
import { createTRPCClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from 'server.ts';
 
const proxy = createTRPCClient<AppRouter>({
links: [
httpBatchLink({
url: 'https://#: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 = proxy.userById.query('id_bilbo', { signal: ac.signal });
 
// 3. Cancel the request if needed
ac.abort();