I'm currently trying to create a for loop inside the "Code by Zapier" action. The intention of the code is to have a for-loop that executes 5 times; each time the loop triggers I want to hit a webhook.
Once the for loop is finished executing, I put a line of code using the callback function in order to exit the action successfully (not ending with a callback function triggers the error "You must return a single object or array of objects").
However, if I finish the action with the callback, then the webhook never gets hit/triggered (as if the fetch function is never executed or doesn't get to finish). If I don't use callback at the end (and use something like "return output = {data: data};", I get a "ReferenceError: output is not defined" error but the fetch request is executed and the webhook gets hit 5 times.
The code I use looks as follows:
var url = "https://hooks.zapier.com/hooks/catch/5506184/byko7zw/";
var i;
for (i = 0; i < 5; i++) {
var headers = {
"Content-type": "application/json",
};
var body = JSON.stringify({ data: "Hello, World!", number: i });
fetch(url, { method: "POST", headers: headers, body: body }).then((res) => {
console.log(res);
})
.catch(callback);
}
callback(null, {});
Would there be a way to successfully hit the webhook the designated number of times in the for loop and exit successfully instead of just one or the other? Any help or advice would be greatly appreciated!