[英]How to slowdown making requests?
I have problem with too fast requesting.我有请求太快的问题。 When my script makes too many requests per second Google throws an error
net::ERR_INSUFFICIENT_RESOURCES
当我的脚本每秒发出太多请求时,Google 会抛出错误
net::ERR_INSUFFICIENT_RESOURCES
I want to make one request per 20ms.我想每 20 毫秒发出一个请求。 How I can achieve that?
我怎样才能做到这一点?
This is how my main function look at this moment...这就是我的主 function 此刻的样子......
const symbolsArr = reader.result.split("\n").map((str) => str.trim()); //symbolsArr is just rergular array, except it's source is from .txt file
function loopFunction(error_symbol) {
for (const symbol of symbolsArr) {
setTimeout(getData(symbol), 5000); //I tried to use setTimeout but it not helps
}
return console.log(error_symbol);
}
loopFunction(error_symbols);
And my fetcher...还有我的抓人...
error_symbols = [];
function getData(symbol) {
fetch(
`https://cors-anywhere.herokuapp.com/` +
`https://eodhistoricaldata.com/api/fundamentals/${symbol}?api_token= (don't look at my secret token :))`,
{
method: "get",
}
)
.then((res) => {
if (res.ok) {
return res.json();
} else {
throw new Error(`Symbol ${symbol} is 'empty'`);
}
})
.then((data) => {
console.log(data);
var myJSON = JSON.stringify(data);
saveFile(myJSON, `${symbol}-json.txt`, "text/plain");
})
.catch((error) => {
error_symbols.push(symbol);
throw error + symbol;
});
}
Pretty simple, I have to somehow put cooldown on fetcher很简单,我必须以某种方式为fetcher 设置冷却时间
try this试试这个
const symbolsArr = reader.result.split("\n").map((str) => str.trim()); //symbolsArr is just rergular array, except it's source is from .txt file
async function loopFunction(error_symbol) {
for (const symbol of symbolsArr) {
await setTimeout(getData(symbol), 5000); //I tried to use setTimeout but it not helps
}
return console.log(error_symbol);
}
loopFunction(error_symbols);
the fetch api is using async promises to handle requests. fetch api 使用异步承诺来处理请求。
i am pretty sure u need to set a await to make the loop wait on each loopin till the fulfillment is done.我很确定你需要设置一个 await 来让循环在每个循环上等待,直到完成完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.