Skip to content
tRPC

Partners

Function: safe()

On this page

safe<TData, TErrorShape>(promise): Promise<SafeResult<TData, TErrorShape>>

Defined in: packages/client/src/safe.ts:47

Awaits a client .query() or .mutate() call without throwing, and returns a [data, error] pair typed with the procedure's error shape.

Type Parameters

Type Parameter
TData
TErrorShape

Parameters

ParameterType
promiseTRPCPromise<TData, TErrorShape>

Returns

Promise<SafeResult<TData, TErrorShape>>

Example

const [post, error] = await safe(client.post.byId.query({ id }));
if (error) {
  console.error(error);
}

const id = post.id;

See

https://trpc.io/docs/client/vanilla/error-handling#errors-as-values-with-safe