...
Code Block | ||
---|---|---|
| ||
// Display's word hello in browser console.
console.log('hello');
// Show in the console object data from crm
const payload = await api.crmManager.getPayload();
console.log(`This is data from the payload ${payload}`); |
...
Promises/Async (await, then)
Groovy JavaScript Async is a niche topic and may be a completely new concept to you. But it's everywhere in JavaScriptnot common in Groovy, and most likely you never heard about it.
Async and Sync
Synchronous: Operations are executed in sequence. Each operation must finish before the next one starts.
Asynchronous: Operations can be executed in parallel. The program doesn't wait for an operation to finish before moving to the next one.
Why to use :
Using Promises: The program continues executing while waiting for the async operation, hence "End" is logged before the result.
Using async/await: It allows writing asynchronous code that looks synchronous, making it easier to read and understand.
Asynchronous operations are crucial for handling time-consuming tasks (like Asynchronous operations are crucial for handling time-consuming tasks (like API calls or file operations) without blocking the main thread of execution.
In Interceptors, JavaScript await
and then
are used to handle data fetch from CRM and other sources.
Await
await
is a keyword that can be used inside an async
function to pause the execution of the function until a Promise is resolved or rejected. This allows us to write asynchronous code in a more synchronous-looking way, making it easier to understand and maintain.
Here's an example of using await
to get payload from CRM:Also, all API calls are async, so If you would use an async function in sync method you will get Promise<pending>
instead of a result.
Code Block | ||
---|---|---|
| ||
export const fetchDataquotesDetailNew = async (crmManager{ api, quoteAPI }) => { const response = await api.crmManager.getPayload(); console.log(response); } fetchData(); // { name: 'Zalgiris', id: '19860815', ... } } |
Without await
, you would get unfulfilled
promise which doesn't have anything to return or show:
; |
Code Block | ||
---|---|---|
| ||
export const fetchDataquotesDetailNew = async (crmManager{ api, quoteAPI }) => { const response = api.crmManager.getPayload(); console.log(response); } fetchData(); // Promise<Pending> // This happened because console.log accessed response // while it is still being executed }; |
Then
then
is a method alternative to await
used to handle the resolution or rejection of a Promise by providing callback functions to be executed when the Promise is resolved or rejected, allowing for the chaining of asynchronous operations.
...
Here's an example of using then
to handle getPayload:
Code Block | ||||
---|---|---|---|---|
| ||||
export const quotesDetailNew = ({ api, quoteAPI }) => { api.crmManager.getPayload() .then((result) => { console.log(result); }) } |
Here is same code but with await
Code Block | ||
---|---|---|
| ||
export const quotesDetailNew = async .catch((error({ api, quoteAPI }) => { const payload = await api.crmManager.getPayload() console.log(errorpayload); }); |
try...catch...finally
Between Groovy and JavaScript try...catch...finally
works same. Here is example for JS
...