I am mapping over a list of 600 URLs and then making Http GET requests. currently the server kicks me out because of making too many requests.
I googled and found this library GitHub - vercel/async-sema: Semaphore using `async` and `await` it allows to throttle the get requests from fetch.
The example which they have on their site is
const { RateLimit } = require('async-sema');
async function f() {
const lim = RateLimit(5); // rps
for (let i = 0; i < n; i++) {
await lim();
// ... do something async
}
}
I translated it to rescript like this
open Js.Promise2
open Belt.Array
let urlList = ["a", "b"]
@module("async-sema")
external rateLimit: int => promise<unit> = "RateLimit"
urlList
-> map(url =>
rateLimit(1)
-> then(_ => url -> Fetch.get -> then(Response.json))
)
My code compiles and runs (yay!) but it doesnβt throttle anything and all requests are made at the same time. While the issue could be in the library itself and I can log an issue at their GitHub site. I want to know did I translate the javascript code correctly?