[英]How to rate limit a for each request
我正在調用 Webflow CMS API,它的速率限制為每分鍾 60 個請求。 因為我有 300 個項目要更新/創建,所以我想知道如何減慢 forEach 的速度以滿足速率限制:
static update(collectionData, country, res) {
return collectionData.get()
.then(docs => {
return docs.forEach(doc => {
setTimeout(() => {
let country = doc.data();
return axios.request({
url: country.webflowId ? (webflowCollection + '/' + country.webflowId) : webflowCollection,
method: country.webflowId ? 'patch' : 'post',
headers: webflowHeaders,
data: {
"fields": {
"name": country.country,
"slug": country.country.replace(/\s+/g, '-').replace("'", "-").toLowerCase(),
"country-code": country.countryCode,
"_archived": false,
"_draft": false
}
}
})
.then(function (response) {
console.log('Updated ' + country.country + " (id: " + response.data._id + ")");
return doc.ref.set({
webflowId: response.data._id
}, {merge: true});
})
.catch(function (response) {
console.log(JSON.stringify(response));
return res.send(JSON.stringify(response));
});
});
})
.then(() => {
return res.status(200).send('Update complete!');
})
.catch(err => {
return res.status(404).send('Error listing countries: ' + err);
});
},2000);
}
我試過 await 函數和 setTimeout 但似乎沒有任何效果。 代碼通過電源並達到限制,然后返回 429 錯誤。
你也許應該看看 axios-rate-limit 模塊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.