Implement Asynchronous and Synchronous Methods

This topic applies to

Applies to

SuiteCommerce Web Stores

 

The Extensibiity API handles tasks synchronously in the frontend and both synchronously and asynchronously in the backend (the backend includes two versions of each method). Asynchronous methods uses the jQuery Deferred Object API. This API provides an easy way to register callbacks and monitor the success or failure of data transactions.

All asynchronous methods return a promise through the deferred object. A promise represents the eventual result of any asynchronous process. Any method that returns a deferred object is an asynchronous method.

The extensibility API implements the deferred object using the jQuery.deferred() method. Whenever an asynchronous operation runs, it returns a deferred promise.

This utility object allows you to relay the success or failure state of the operation and continue with your desired flow of execution.

Returning the Deferred object gives you the flexibility to use the promises returned by the default Deferred object of the Extensibility API or the jQuery.deferred() method. For more information about these promises, see https://api.jquery.com/category/deferred-object and www.promisejs.org.

Note

The implementation of deferred is similar to standard promises, except that it uses .fail() instead of .catch().


jQuery.deferred()

To use the promises returned by the jQuery library, you must instantiate jQuery.deferred() in your extension code.

For example, you might use jQuery.Deferred().resolve() to return a resolved promise in your code, especially in cases when you cannot use Backbone to implement asynchronous behavior.

The following example shows the instantiation in a JavaScript (frontend) or SuiteScript (backend) file:

define('MyExtension', ['jQuery'], function(jQuery) {
  var promise = jQuery.deferred();
})