How to execute multiple HTTP requests in Zapier Code action with proper completion

I’m working on a Zapier Code action where I need to make several HTTP requests in a loop. My goal is to send 5 requests to an endpoint, but I’m having trouble getting both the requests to execute AND the action to complete properly.

When I use the callback function at the end, the action completes without errors but the HTTP requests never get sent. When I don’t use the callback, the requests work fine but I get a “ReferenceError” and the action fails.

Here’s my current approach:

var endpoint = "https://webhook.site/abc123def";
var counter;
for (counter = 0; counter < 5; counter++) {
  var requestHeaders = {
    "Content-Type": "application/json",
  };

  var payload = JSON.stringify({ message: "Test data", index: counter });

  fetch(endpoint, { method: "POST", headers: requestHeaders, body: payload }).then((response) => {
      console.log(response);
    })
    .catch(callback);
}

callback(null, {});

Is there a way to ensure all the HTTP requests complete before calling the callback function? I need both the requests to succeed and the Zapier action to finish properly.

you need to wait for all promises to finish before calling callback. try using Promise.all() to wrap your fetch requests - it’ll wait for everything to complete then you can call callback in the .then() block. your current code calls callback immediately without waiting

The issue you’re encountering is classic async behavior - your callback executes immediately while the fetch requests are still pending. I ran into this exact problem when building webhook integrations last year. Instead of calling the callback right after the loop, you need to collect all your promises first. Try storing each fetch in an array, then use Promise.all to wait for completion. Something like this approach worked for me: create an empty promises array, push each fetch into it during the loop, then Promise.all(promises).then(() => callback(null, {})).catch(callback). This ensures Zapier waits for all HTTP requests to finish before marking the action as complete.

I’ve dealt with similar timing issues in Zapier workflows. The root cause is that you’re calling callback() synchronously while your HTTP requests are executing asynchronously. One alternative approach is to use async/await pattern which can be more readable than Promise.all. You could wrap your code in an async function and use await for each fetch request, or better yet, use Promise.allSettled() instead of Promise.all() if you want to ensure all requests complete even if some fail. This way you get better error handling and can still process successful responses. Just remember to handle the promise resolution properly before invoking the callback function.