SuiteScript 2.x Promise APIs for Asynchronous Programming
SuiteScript 2.x introduces Promise APIs, enhancing asynchronous coding. Learn about usage, best practices, and example implementation.
TL;DR
SuiteScript 2.x introduces Promise APIs, allowing developers to handle asynchronous operations intuitively and efficiently. Promises facilitate managing the success or failure of processes while the script continues to run other segments, ultimately improving performance and readability.
What Are Promises?
A promise is a JavaScript object representing the eventual success or failure of an asynchronous operation. When created, a promise has one of three states:
fulfilled: The operation completed successfully.rejected: The operation failed.pending: The operation is still in progress.
Once a promise is resolved or rejected, it cannot change again. Callbacks (.then(), .catch()) associated with the promise are executed upon completion, allowing for clean and efficient asynchronous code.
SuiteScript and Promises
SuiteScript 2.x supports promises across various modules. Starting with NetSuite 2021.1, certain server scripts also incorporate promise functionality. This enables both client and server scripts to manage asynchronous processes effectively.
Supported Modules and APIs
Below is a selection of some modules in SuiteScript 2.x that support Promise APIs:
| Module | Promise API | Client Scripts | Server Scripts |
|---|---|---|---|
| N/action | Action.promise(options) | ✅ | - |
| N/currentRecord | currentRecord.get.promise() | ✅ | - |
| N/http | http.get.promise(options) | ✅ | ✅ |
| N/email | email.send.promise(options) | ✅ | - |
| N/search | search.create.promise(options) | ✅ | ✅ |
| N/transaction | transaction.void.promise(options) | - | ✅ |
Best Practices for Asynchronous Programming
When utilizing promises in SuiteScript, consider the following best practices:
- Always include a
.catchhandler for promise rejections. - Chain promises rather than nesting them for better readability and performance.
- Keep promise chains concise to save on memory and CPU usage.
- Utilize
promise.finallyfor code that must execute whether the promise is fulfilled or rejected. - For parallel asynchronous operations, use
promise.all. - Avoid mixing synchronous and asynchronous APIs within the same function.
Example: Basic Usage of Promises
The following example illustrates the use of the search.create.promise API in a client script:
1/**2* @NApiVersion 2.x3*/4 5define(['N/search'], function(search) {6 function doSomething() {7 search.create.promise({8 type: 'salesorder'9 })10 .then(function(result) {11 log.debug("Completed: " + result);12 // Perform actions after completion13 })14 .catch(function(reason) {15 log.debug("Failed: " + reason);16 // Handle failure17 });18 }19 return {20 pageInit: doSomething21 };22});Custom Promises
Custom promises can be created in SuiteScript, allowing for additional flexibility beyond the provided Promise APIs:
1/**2* @NApiVersion 2.x3*/4 5define(function() {6 function doSomething(addresses) {7 var promise = new Promise(function(resolve, reject) {8 var url = 'https://your.favorite.maps/api/directions?start=' + addresses.start + '&end=' + addresses.end,9 xhr = new XMLHttpRequest();10 11 xhr.addEventListener('load', function(event) {12 if (xhr.status === 200) {13 resolve(xhr.responseText);14 } else {15 reject(xhr.statusText);16 }17 });18 19 xhr.open('GET', url, true);20 xhr.send();21 });22 return promise;23 }24 25 return {26 lookupDirections: doSomething27 };28});Key Takeaways
- SuiteScript 2.x enhances asynchronous programming with Promise APIs.
- Promises help manage asynchronous operations, improving script readability.
- Best practices for using promises include avoiding nesting and using
.catchfor rejections.
Frequently Asked Questions (4)
Do I need to enable a feature flag for using Promise APIs in SuiteScript 2.x?
What modules in SuiteScript 2.x support Promise APIs?
Can I use Promise APIs with all scripts in NetSuite?
What are best practices for managing rejections in SuiteScript Promises?
Was this article helpful?
More in SuiteScript
- Common SuiteScript Errors and Solutions for NetSuite
Common NetSuite script errors include INVALID_SCRIPT_DEPLOYMENT_ID and SSS_AUTHORIZATION_HEADER_NOT_ALLOWED. Learn effective solutions.
- Set Sublist Field Values in SuiteScript 2.x for Record Management
Set sublist field values in SuiteScript 2.x for effective record management using standard and dynamic modes.
- Setting Field Values in SuiteScript for Effective Record
Learn to set field values in SuiteScript effectively, troubleshooting common errors and understanding data types.
- SuiteScript 2.1 Enhancements and API Updates in NetSuite
SuiteScript 2.1 enables execution of 2.0 scripts and supports PATCH method for enhanced API capabilities.
Advertising
Reach SuiteScript Professionals
Put your product in front of NetSuite experts who work with SuiteScript every day.
Sponsor This Category