Update: I implemented a .then() as suggested by Shubham, there were two good suggestions and for anyone who reads this in future this is a good article comparing them: https://beyondthecloud.dev/blog/then-vs-async-await-in-lwc

In short: using async/await forces the call to be synchronous, and then/catch just makes whatever is in then to wait for the first call.

I'm building an import for records from a CSV. When handling smaller files the order of execution is incorrect. This is an LWC being used as a Lightning Quick Action.

First thing is a call to an Apex method to delete the existing records associated with the account this lightning quick action was called on.

Secondly I chunk the file and send those chunks to an Apex method for processing and storing.

Even though the call to delete is first the execution log shows that the "Upload Chunk" call was processed first. It is important that the records are deleted first. Otherwise some records could be uploaded then immediately deleted.

I know I can just add a delay before the import starts to allow the first call to complete, but I want to see what other options there are.

Code to handle parsing file

Dev Console Execution Log

2 Answers 2


What you can do is call your deletePartnerMembers apex function with .then() & .catch() & do your further processing in then-callback like this-

deletePartnerMembers ({partnerId: self.recordId})
  .then (() => {
    // code to further processing
  .catch (() => {
    //error handling

It'll wait for deletePartnerMembers to get executed completely.

  • This is technically correct but does not say why. See The Fox's alternative answer to see how providing some background info helps.
    – Phil W
    Commented Feb 11, 2023 at 8:11

Calling Apex is asynchronous, meaning that the JavaScript doesn't wait for the Apex to finish while it continues. You can use the async keyword on the function, then await the method until it is done.

async handleSubmit(event) {
  // other code here
  try {
    // now await forces the script to wait for this to finish
    await deletePartnerMembers({ partnerId: self.recordId });
  } catch(error) {
    // error will contain more details in case your Apex failed to execute
  • Thank you for the explanation, I did actually implement the .then() suggestion from Shubham in this scenario after comparing the two in this scenario I don't necessarily want to force the whole process to be synchronous, but I do need to force the order of these 2 calls.
    – Alex
    Commented Feb 14, 2023 at 20:00
  • @Alex You could make Papa Parse and the delete call asynchronous together, something like await Promise.all([deletePartnerMembers({ partnerId }), parseCsv(file)]) by wrapping the parser as in this answer. I just don't like the then/catch system for its verbosity, but as long as you're happy with what you've got, then that's all that really matters.
    – sfdcfox
    Commented Feb 14, 2023 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .