Use async/await For Asynchronous Processing
Overview
This article introduces how to use async/await instead of Promise to simplify asynchronous processing in Kintone customizations.
What Is async/await?
async/await is a syntax that executes asynchronous operations, such as API calls, in order from top to bottom. It enables retrieving data, storing values in variables, and waiting for processes to complete.
Practical examples are introduced later in this article.
async/await is built on Promise objects and can be considered a more convenient way to write Promise-based code.
Issues With Promise
Promise has the following drawbacks:
- More difficult to understand than regular code
Promise is a mechanism for handling asynchronous processing, but it can be harder to understand than regular code.
Using methods likethen()andcatch()can make the code flow less clear. - Sequential asynchronous processing requires multiple
then()calls and becomes harder to read
With Promise, chaining asynchronous operations requires connecting multiplethen()calls.
This can lead to deep nesting and reduced readability.
Due to JavaScript specifications, synchronous waiting for responses is not possible in asynchronous operations such as API calls.
For this reason, mechanisms like Promise are needed to handle asynchronous results.
The following is an example of retrieving three records in series using Promise.
|
|
Benefits of async/await
Using async/await provides the following benefits:
- More concise and intuitive than Promise-based asynchronous code.
- No need to chain multiple
then()calls when running asynchronous operations in sequence. - Based on Promise, so methods like
.then()and.all()remain available.
The following compares code written with Promise to code written with async/await.
-
Retrieve a single record when the Record Details page is opened using standard Promise.
1 2 3 4 5kintone.events.on('app.record.detail.show', () => { return kintone.api('/k/v1/record.json', 'GET', {app: 1, id: 1}).then((resp) => { console.log(resp); // Display the retrieved content }); }); -
Retrieve a single record when the Record Details page is opened using async/await.
1 2 3 4kintone.events.on('app.record.detail.show', async () => { const resp = await kintone.api('/k/v1/record.json', 'GET', {app: 1, id: 1}); console.log(resp); // Display the retrieved content });
This syntax stores the API response in the resp variable, just like a regular variable declaration.
When using await, prefix the enclosing function with async to indicate it is an async function.
An async function returns a Promise.
This allows Kintone to wait for the result.
How to Use async/await
Declare async at the beginning of the function, and declare await at each location where the process should wait.
Note that forgetting to add async is a common mistake.
Actual usage in Kintone is demonstrated in the examples below.
|
|
MDN also provides examples for reference.
MDN Web Docs:
async function
Examples
The following sections show additional specific examples.
For comparison, both Promise-based and async/await-based versions are provided.
Example 1: Set Default Values From Another Record on the Record Edit Page
Example using Promise
|
|
Example using async/await
|
|
Use the try/catch syntax for error handling.
Refer to the following article for details on the errors returned by kintone.api():
Kintone REST API Overview - HTTP Status Codes
Example 2: Retrieve Three Records and Sum Their Values on Record Save
Example using Promise
|
|
Example using async/await
Chained operations written this way become much easier to follow.
|
|
When To Consider Promise
Using async/await does not eliminate the need to understand Promise. As noted above, async/await simply makes asynchronous code easier to write.
In some cases, using Promise may be more suitable than async/await. For example, when executing multiple asynchronous operations in parallel, using Promise.all() can be more efficient than awaiting each operation sequentially.
|
|
When writing code like the example above, unifying with async/await is recommended.
|
|
Promise and async/await have the following relationship:
awaitcan be used to wait for a Promise to complete.
As shown in the earlier examples,kintone.api()can be awaited.
This is becausekintone.api()returns a Promise object.- An async function returns a Promise.
An async function returns a Promise object.
This is whythenandawaitcan be combined, as shown above.
However, mixing these styles excessively causes confusion and is generally discouraged.