I need help with running a loop in Zapier’s Code action that makes HTTP requests to a webhook endpoint.
I want to create a loop that runs several times and each time it should send a request to a webhook URL. After the loop finishes, I need the action to complete without errors.
The problem I’m facing is weird. When I end my code with the callback function (which Zapier needs to finish successfully), the HTTP requests never actually get sent to the webhook. But if I don’t use the callback and try to return data directly, I get an error saying my variable isn’t defined, although the webhook does receive all the requests.
Here’s what my code looks like:
var endpoint = "https://hooks.zapier.com/hooks/catch/1234567/abc123d/";
var counter;
for (counter = 0; counter < 5; counter++) {
var requestHeaders = {
"Content-type": "application/json",
};
var payload = JSON.stringify({ message: "Test data", iteration: counter });
fetch(endpoint, { method: "POST", headers: requestHeaders, body: payload }).then((response) => {
console.log(response);
})
.catch(callback);
}
callback(null, {});
Is there a way to make sure all the webhook calls complete before the Zapier action finishes? I need both things to work - the requests should go through and the action should end properly.
This is a timing issue. Zapier kills the execution context right after the callback runs, which cuts off your HTTP requests mid-flight. I hit the same problem building a data sync integration last year. You need to collect all your promises first, then wait for them to finish:
var endpoint = "https://hooks.zapier.com/hooks/catch/1234567/abc123d/";
var promises = [];
for (var counter = 0; counter < 5; counter++) {
var requestHeaders = {"Content-type": "application/json"};
var payload = JSON.stringify({message: "Test data", iteration: counter});
var request = fetch(endpoint, {
method: "POST",
headers: requestHeaders,
body: payload
});
promises.push(request);
}
Promise.all(promises)
.then(responses => {
callback(null, {completed: responses.length});
})
.catch(err => {
callback(err);
});
This makes Zapier wait for all webhook calls to finish before marking the action complete. Batch the promises and use Promise.all() to sync everything up.
u should try using Promise.all() to wait for all the fetch calls to finish before calling your callback. Create an array for your fetch promises, and when they’re all done, call the callback. rn it seems like you’re ending the action too soon.
This happens because fetch() returns a promise, but your callback runs right away without waiting for the HTTP requests to finish. Zapier sees the callback and kills the execution before your async requests complete. I’ve hit this exact issue before. Fix it with async/await:
var endpoint = "https://hooks.zapier.com/hooks/catch/1234567/abc123d/";
var requests = [];
for (var counter = 0; counter < 5; counter++) {
var requestHeaders = {"Content-type": "application/json"};
var payload = JSON.stringify({
message: "Test data",
iteration: counter
});
requests.push(fetch(endpoint, {
method: "POST",
headers: requestHeaders,
body: payload
}));
}
Promise.all(requests)
.then(() => {
callback(null, {success: true});
})
.catch((error) => {
callback(error);
});
This ensures all webhook calls finish before Zapier considers the action done.
Been there. Zapier’s Code action cuts off async operations when the callback fires - super annoying.
Why fight Zapier’s weird execution model though? I switched to Latenode for this stuff and haven’t looked back.
Latenode doesn’t make you mess with Promise.all() workarounds or callback timing. Just drag an HTTP Request node, connect it to a Loop node, and it handles async automatically. No code required.
Built something similar last month - had to ping 20+ endpoints in sequence. Took 5 minutes in Latenode vs hours debugging callback hell in Zapier.
The visual flow shows exactly what’s happening, plus you get proper error handling for each request without try-catch blocks.
Check it out: https://latenode.com