JavaScript is a single-threaded language, which means it can only execute one line of code at a time. Asynchronous operations, such as fetching data from a server or waiting for user input, can block the main thread of execution and make the application unresponsive.
Promises allow us to handle asynchronous operations in a non-blocking way. With promises, we can initiate an asynchronous operation and attach a callback function to handle the result when it becomes available. This allows the main thread of execution to continue with other tasks while the asynchronous operation is being performed in the background.
Promise chaining and async/await are two approaches to handle asynchronous operations.
Promise Chaining
In promise chaining, we use the then()
method to chain multiple asynchronous operations. This is useful when we have multiple asynchronous operations that need to be performed in a specific order.
Async – Await
Async/await allows us to write asynchronous code that looks and behaves like synchronous code. The async
keyword is used to declare a function as asynchronous, and the await
keyword is used to wait for the result of a promise.
Here is an example of using Promise chaining and Async/Await to fetch data from Apex in a Lightning Web ComponentPromise Chaining Example:
import { LightningElement } from 'lwc'; import fetchDataFromApex from '@salesforce/apex/MyController.fetchDataFromApex'; export default class MyComponent extends LightningElement { connectedCallback() { fetchDataFromApex() .then(data => { // Handle data from first Apex call return fetchDataFromApex({ param: data.id }); }) .then(result => { // Handle data from second Apex call }) .catch(error => { // Handle errors }); } }
In this example, we call the Apex method fetchDataFromApex() in the connectedCallback() lifecycle hook of the component.
We use the then() method to handle the data from the first Apex call and return the result to fetchDataFromApex() with the data.id value as a parameter to second apex call . Finally, we handle any errors that might occur using a catch() block.Async/Await Example:
import { LightningElement } from 'lwc'; import fetchDataFromApex from '@salesforce/apex/MyController.fetchDataFromApex'; export default class MyComponent extends LightningElement { async connectedCallback() { try { const data = await fetchDataFromApex(); // Handle data from first Apex call const result = await fetchDataFromApex({ param: data.id }); // Handle data from second Apex call } catch (error) { // Handle errors } } }
First, we call the Apex method fetchDataFromApex() using the await keyword, which waits for the response before continuing execution. The response is stored in the data variable. Next, we handle the data from the first Apex call in the code block following the first await statement.
We then call fetchDataFromApex() again, passing data.id as a parameter, and wait for the response to be returned using another await statement. The response is stored in the result variable.
Finally, we handle the data from the second Apex call in the code block following the second await statement. If an error occurs during the execution of the asynchronous code, the catch block is executed, and we can handle the error accordingly.