Unlocking Efficiency with Async Functions in NetSuite Server-Side SuiteScript

By: Bobby Stevens

The introduction of SuiteScript 2.1 brought the power of asynchronous programming to NetSuite’s server-side development. By using async, await, and promises, you can streamline your code, boost performance, and enhance the user experience.

Why Async Matters

  1. Optimized Resource Usage: Asynchronous functions prevent your code from blocking while waiting for operations like searches or external API calls to complete. This allows NetSuite to allocate resources more intelligently, potentially leading to faster overall execution of your scripts.
  2. Improved Responsiveness: In scenarios where your scripts interact with the user interface, asynchronous operations help prevent the UI from freezing during long-running processes, maintaining a fluid user experience.
  3. Cleaner Code: The async/await syntax often leads to more readable code than traditional callback-based approaches, especially when handling multiple asynchronous tasks.

Supported Modules

SuiteScript 2.1 offers asynchronous support for a core set of modules:

  1. N/http, N/https: Fetch data from external services or make API calls without halting your script’s execution.
  2. N/query: Efficiently run database queries in the background.
  3. N/search: Optimize the loading and execution of multiple searches.
  4. N/transaction: Handle record creation, updates, and deletions asynchronously.


Let’s illustrate with an example:

async function loadSearches() {
  let mySearchOne = search.load.promise({
    type: search.Type.SALES_ORDER,
    id: 'customsearch_txn_search_salesorder'
  });

  let mySearchTwo = search.load.promise({
    type: search.Type.OPPORTUNITY,
    id: 'customsearch_txn_search_salesorder'
  });

  // Wait for both searches to complete concurrently
  let searchObjs = await Promise.all([mySearchOne, mySearchTwo]);

  return searchObjs;
}

Explanation

  1. async function loadSearches(): The async keyword marks the function as asynchronous, signaling that it returns a Promise.
  2. search.load.promise(): This NetSuite function initiates searches asynchronously, returning Promises for each.
  3. Promise.all([mySearchOne, mySearchTwo]): This is the key! Promise.all takes an array of Promises and returns a new Promise that resolves when all the input Promises resolve, or rejects if any of them reject.
  4. await: The await keyword pauses the execution of loadSearches until the combined Promise from Promise.all resolves, providing you with the results of both searches in the searchObjs array.

When to Use Asynchronous Functions

Identify areas in your SuiteScripts where long-running operations, especially multiple ones that can run independently, might cause bottlenecks. These are excellent candidates for refactoring with asynchronous patterns.

Need Help? Let’s Connect!

I’m a certified NetSuite developer dedicated to making NetSuite work seamlessly for businesses. If you have any NetSuite development requirements, I’d be delighted to assist! Please feel free to reach out.

← Back

Thank you for your response. ✨

Warning
Warning
Warning
Warning.