admin管理员组文章数量:1316008
Description
What is the best way to call api if i wanted to call it multiple times. while ensuring api overhit (excessive api call hit per second), which delay technique is best to apply, best way to code it while keeping space and time complexity utilized at minimal as it can be?
* for intance below is also causes situation where in my mern app i had to call api in multiple times. i.e in loop to fullfill my needs.*
/docs/api/admin-rest#rate_limits
CODE to refactor as per description
if (getEssentials.endpoint === "image") {
for (let i = 0; i < getEssentials.payload.length; i++) {
// console.log("aksjdfh", getEssentials.payload[i].media);
try {
const options = {
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json",
authorization: "Bearer .......................",
},
body: JSON.stringify({
media: `${getEssentials.payload[i].media}`,
}),
};
// console.log("checkojign", options);
const response = await fetch(
`/messages/media/${
getEssentials.endpoint
}?caption=${encodeURIComponent(
getEssentials.payload[i].caption
)}&to=${to}`,
options
);
const data = await response.json();
console.log(`Image ${i + 1} sent:`, data);
await delay();
} catch (error) {
console.error(`Error sending image ${i + 1}:`, error);
}
}
return { message: "All images sent successfully" }; // Return a success message after all images are sent
}
I need to optimize the code. which utilizes minimal resources of backend app. also ensuring api won't overhit while ensuring all api calls should return the response regardless of thier response status.
本文标签: nodejsMultiAPICALLINJavaScriptStack Overflow
版权声明:本文标题:node.js - Multi-API-CALL-IN-JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971633a2407875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论