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.
Source: This article is based on Oracle's official NetSuite documentation.
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
- SuiteScript 2.1 Enhancements in NetSuite February Updates
SuiteScript 2.1 now supports async features and PATCH method. Discover the latest API and SuiteProcurement improvements.
- Custom Tool Script Enhancements in NetSuite
Custom tool scripts in NetSuite gain execution log support and a new management page in February 16, 2026.
- Scheduling Map/Reduce Script Deployments in NetSuite
Learn to schedule map/reduce script submissions, including one-time and recurring options in NetSuite.
- API Governance Units Calculation in NetSuite 2026.1
NetSuite 2026.1 introduces examples illustrating API governance unit calculations for both user event and scheduled scripts.
