How to define an async procedure?

export const exampleRouter = createTRPCRouter({
hello: publicProcedure
.input(z.object({ text: z.string() }))
.query(({ input }) => {
return {
greeting: `Hello ${input.text}`,
};
}),

getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.example.findMany();
}),

getSecretMessage: protectedProcedure.query(() => {
return "you can now see this secret message!";
}),
initAgones: publicProcedure.query(async () => {

const connectRes = await agonesSDK.connect();
console.log({connectRes});

return 'test';
})
});
export const exampleRouter = createTRPCRouter({
hello: publicProcedure
.input(z.object({ text: z.string() }))
.query(({ input }) => {
return {
greeting: `Hello ${input.text}`,
};
}),

getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.example.findMany();
}),

getSecretMessage: protectedProcedure.query(() => {
return "you can now see this secret message!";
}),
initAgones: publicProcedure.query(async () => {

const connectRes = await agonesSDK.connect();
console.log({connectRes});

return 'test';
})
});
i'm trying to connect to an SDK asynchronously inside a procedure and it seems to break the entire router, how do i do async things in the procedure?
5 Replies
Neto
Neto2y ago
getAll: publicProcedure.query(async ({ ctx }) => {
return ctx.prisma.example.findMany();
}),
getAll: publicProcedure.query(async ({ ctx }) => {
return ctx.prisma.example.findMany();
}),
add async after the first ( just like the initAgones
functionthatreturnsfalse
do i have to make ALL of them async?
Neto
Neto2y ago
no only if you need to use async code on the procedure
functionthatreturnsfalse
if i make my procedure async, it seems to cause the request to hang on the client
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server