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