As we all know that all actions from Lightning components are sent to server in asynchronous manner and we can not predict the sequence about response from server.
If we have requirement to wait for all operations to complete and then perform some action on lightning components like hiding the spinner or displaying all data at once.
We can either queue the first server call and when we get response then we perform second server call. For this you can use Javascript Promise pattern to sequence the server call. Please refer below URL for complete understanding of sequencing of server call using javascript promise.
JavaScript Promises vs Callback Functions in Lightning Components
Now if we need to fire server calls in parallel and want to wait for all response, then you can utilize promise.all methods to achieve this. This will execute many promises in parallel and wait until all of them are ready. Below is syntax to use promise.all:
Note:
Below is complete snippet for your reference:
Hope this will help!!
If we have requirement to wait for all operations to complete and then perform some action on lightning components like hiding the spinner or displaying all data at once.
We can either queue the first server call and when we get response then we perform second server call. For this you can use Javascript Promise pattern to sequence the server call. Please refer below URL for complete understanding of sequencing of server call using javascript promise.
JavaScript Promises vs Callback Functions in Lightning Components
Now if we need to fire server calls in parallel and want to wait for all response, then you can utilize promise.all methods to achieve this. This will execute many promises in parallel and wait until all of them are ready. Below is syntax to use promise.all:
//create array of promise and pass it to promise.all
var promiseArray =[promise1, promise2];
var combinedPromise = Promise.all(promiseArray);
//this return single promise. Now handle it with then and catch
combinedPromise
.then($A.getCallback(function(results){
//handle success
var promise1Results = results[0]; //Results from Promise 1
var promise2Results = results[1]; //Results from Promise 2
}))
.catch($A.getCallback(function () {
//Handle errors on any promise here
console.log('Some error has occured');
}));
Note:
- Promise.all takes an array of promises and returns a new promise.
- The new promise resolves when all listed promises are settled, and the array of their results becomes its result.
- Order of the resulting array members is the same as in its source promises. Even though the first promise takes the longest time to resolve, it’s still first in the array of results.
- If any of the promises is rejected, the promise returned by Promise.all immediately rejects with that error.
- For example, if there are multiple fetch calls and one fails, the others will still continue to execute, but Promise.all won’t watch them anymore. They will probably settle, but their results will be ignored. Promise.all does nothing to cancel them, as there’s no concept of “cancellation” in promises.
- This will help to improve the performance of lightning components as server operations are not performed in sequential order. Due to parallel call to server, lightning components will perform better.
Below is complete snippet for your reference:
Hope this will help!!