Using async fetch requests in Zapier's Code action - callback implementation

I’m working with Zapier’s Code action and trying to make HTTP requests to my CRM system. The platform uses Node.js but I’m having trouble with asynchronous operations.

Here’s what I’m attempting to do:

// Zapier includes node-fetch by default

var access_key = "my_secret_key";
var product_title = "Sample Product";

fetch("https://api.salesforce.com/v2/products/search?name=" + product_title + "&key=" + access_key)
  .then(function(response) {
    return response.json();
  }).then(function(data) {
     var product_id = data.results[0].id;
     console.log("Found product ID: " + product_id);
  }).catch(function(err) {
     console.log("Request failed");
  });

output = {status: "success", message: "complete"};

This works fine when I test it locally, but Zapier throws an error saying I need to use a callback for async operations with fetch. How do I properly handle this asynchronous request in Zapier’s environment?

yeah, zapier needs a callback for async stuff. stick your fetch inside a callback like callback(null, {your: 'output'}); in your final .then() block. without it, zapier will finish running before your fetch completes.

I encountered a similar issue when working with HubSpot and Zapier. The crux of the problem lies in Zapier’s requirement for either synchronous code or proper asynchronous handling through callbacks. In your setup, you’re trying to assign the output variable before the fetch operation finishes, which leads to a failure.

It’s crucial to eliminate the global output variable and manage everything within your Promise chain. Only call the callback once you have the final result. Additionally, ensure proper error handling by passing errors as the first parameter to the callback; otherwise, Zapier won’t recognize that a failure occurred.

One important note: you cannot mix output variables with async callbacks in Zapier, so it’s best to choose one approach. Given that you’re making HTTP requests, utilizing callbacks is the way to go.

The problem is how Zapier handles async operations. Your fetch request runs asynchronously, but Zapier keeps executing the rest of your code (including the output assignment) before the HTTP request finishes. You need to move your output assignment inside the Promise chain and use Zapier’s callback parameter. Here’s the fix: javascript var access_key = "my_secret_key"; var product_title = "Sample Product"; fetch("https://api.salesforce.com/v2/products/search?name=" + product_title + "&key=" + access_key) .then(function(response) { return response.json(); }).then(function(data) { var product_id = data.results[0].id; console.log("Found product ID: " + product_id); callback(null, {status: "success", product_id: product_id}); }).catch(function(err) { console.log("Request failed"); callback(err); }); Remove the output assignment at the bottom - the callback now handles returning your data to Zapier. This makes Zapier wait for your async operation to finish before moving on.