I have a use case where I want to make an async call (consider it similar to ajax) and then in success block of that call I want to make a series of async calls in a loop using the id which is generated by parent call. My requirements are :
- Where do I put the code of showing success toast? Currently I'm putting it after the for loop inside success block, but it has a problem that it will get executed before child async calls are completed because for loop will not wait for the calls and will get executed in no time and code will go to the showing of success toast.
- If any one of the child call fails, then no further calls should happen (this is more from an efficiency point of view), and also in such scenario, I should be able to delete the parent record which was created so how to handle that as well? Thanks in advance!
Sample code snippet :
asyncCallA(inputId)
.then(output => {
// inputIdsForChildCalls is the list of inputIds for child async
// calls
inputIdsForChildCalls = [do something with output]
for (let i = 0; i < inputIdsForChildCalls.length; i++) {
asyncCallB(inputIdsForChildCalls[i])
.then(output => {
// do something
})
.catch(error => {
// do something with error
});
}
showSuccessToast("Records created successfully!");
})
.catch(error => {
// do something with error
});