Working with the callback parameter

The callback function is an additional parameter of each method of the Unica Interact JavaScript API.

The main browser process is a single threaded event loop. Executing a long-running operation within a single-threaded event loop, blocks the process. This is stops the process from processing other events while it waits for your operation to complete. In order to prevent blocks on long-running operations, the XMLHttpRequest provides an asynchronous interface. You pass it a callback to run after the operation is complete, and while it processes, it gives control back to the main event loop instead of blocking the process.

If the method was successful, the callback function calls onSuccess. If the method failed, the callback function calls onError.

For example, if you wanted to display offers on your web page, you would use the getOffers method and use the callback to display on the page. The web page behaves normally and does not wait for Unica Interact to return the offers. Instead, when Unica Interact does return the offers, the response is sent back in the callback parameter. You can parse the callback data and show offers on the page.

You can use one generic callback for all functions or you can also use specific callbacks for specific functions.

You can use var callback = InteractAPI.Callback.create(onSuccess, onError); to create a generic callback function.

You can use the following function to create a specific callback function for the getOffers method.

var callbackforGetOffer = InteractAPI.Callback.create(onSuccessofGetOffer,
onErrorofGetOffer);