Skip to content
Discussion options

You must be logged in to vote

For ofetch with rate limiting, heres a clean approach using p-limit or bottleneck:

Option 1: Using p-limit (simple)

import { ofetch } from "ofetch";
import pLimit from "p-limit";

const limit = pLimit(10); // max 10 concurrent

const rateLimitedFetch = (url, options) => limit(() => ofetch(url, options));

Option 2: Using bottleneck (more control)

import { ofetch } from "ofetch";
import Bottleneck from "bottleneck";

const limiter = new Bottleneck({
  maxConcurrent: 10,
  minTime: 100 // 100ms between requests = 10/sec
});

const rateLimitedFetch = limiter.wrap((url, opts) => ofetch(url, opts));

Option 3: Custom interceptor

let lastRequest = 0;
const minInterval = 100; // 10 req/sec

const api

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@joaopedrodcf
Comment options

Answer selected by joaopedrodcf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants