Backend Asynchronous and Synchronous Methods
Backend components include both asynchronous and synchronous methods. Each asynchronous method also includes a synchronous version with the word Sync
appended to the asynchronous method. For example, the asynchronous method: getLines()
includes a synchronous version: getLinesSync()
.
You can use asynchronous methods to reuse the frontend extension code for the CartComponent on the backend with the BackendCartComponent. The backend methods use the same signature as the frontend methods.
The following example shows the use of both asynchronous and synchronous versions of the cart.getLines()
method in a SuiteScript file:
In this example, MyExtension
, Vendor
, and ExtensionName
are defined when you create a baseline extension using the developer tools.
define('Vendor.ExtensionName.1.0.0.MyExtension'
, [
'Application'
]
, function(
Application
)
{
'use strict';
return {
myMethod: function()
{
var cart = Application.getComponent('Cart');
// asynchronous API
cart.getLines()
.then(function(result)
{
nlapiLogExecution('DEBUG', 'Lines in the cart', JSON.stringify(result));
})
.catch(function(error)
{
nlapiLogExecution('ERROR', 'Error getting lines in the cart', JSON.stringify(error));
});
// synchronous API
var lines = cart.getLinesSync()
}
};
});
The asynchronous methods in the backend currently use SuiteScript 1.0, which does not support asynchronous operations. As a result, although the asynchronous methods return a promise, the actual operation of the method is synchronous.