tsuki
tsuki
Explore posts from servers
TTCTheo's Typesafe Cult
Created by tsuki on 3/12/2024 in #questions
Properly setting a minimum delay when fetching data from external api
hi all. i'm trying to use the scryfall api, and they've got a requirement to allow at least 50-100ms of delay between each request to their api. how would i go about properly implementing that? i tried throwing the "whole" thing in a setTimeout() but that doesn't work as i'd expect, as it doesn't actually wait out the timeout and just returns undefined instantly.
import { z } from "zod";

import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";

export const scryfallRouter = createTRPCRouter({
get: publicProcedure
.input(z.object({ name: z.string() }))
.query(async ({ input }) => {
setTimeout(async () => {
console.log("required delay");

const res = await fetch(
`https://api.scryfall.com/cards/named?fuzzy=${input.name}`,
);

if (!res.ok) {
throw new Error("Failed to fetch card");
}

const data = (await res.json()) as unknown;

console.log(data);
return data;
}, 150);
}),
});
import { z } from "zod";

import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";

export const scryfallRouter = createTRPCRouter({
get: publicProcedure
.input(z.object({ name: z.string() }))
.query(async ({ input }) => {
setTimeout(async () => {
console.log("required delay");

const res = await fetch(
`https://api.scryfall.com/cards/named?fuzzy=${input.name}`,
);

if (!res.ok) {
throw new Error("Failed to fetch card");
}

const data = (await res.json()) as unknown;

console.log(data);
return data;
}, 150);
}),
});
i realize the placeholder post router, has something similar but i'm worried it won't actually delay the fetch call like required. any insights/comments are greatly appreciated :JinxHeart:
4 replies
TTCTheo's Typesafe Cult
Created by tsuki on 11/2/2023 in #questions
onSuccess, onError and onSettled deprecated next major version?
No description
16 replies