0

I have a method written on onclick event on LWC Js for a datatable.

     handleOnclickSelect(event) {
            let accountRec = event.detail.row;
            let targetRowId = accountRec.Id;
            this.checkAccountDetails(targetRowId);
    console.log('Called On Click Method');
    console.log('Result Value ',this.hasresult);
    }
    
     checkAccountDetails(recordId) {
    
            checkAccountDtsRec({
                accountId: recordId
            })
                .then(result => {
             if (result != undefined) {
    // some logic
this.hasresult = true;
    console.log('Logic Inside Method');
    }
    }

while this code is getting executed. I found the console log in the following order -

  • Called On Click Method
  • Result Value undefined
  • Logic Inside Method

Is the cursor not moved to the calling method and once completed then come back to the main method from where it was called ? Is this expected in a promise callback ?

2 Answers 2

1

Server-side calls are implemented as promises in LWC. This means that when the line of code that calls the server runs, the Promise is placed in a queue for later execution. The rest of your code is then run ("Called On Click Method", "Result Value"), then the Promise returns, and then the output "Logic Inside Method" is logged.

There are two ways to deal with this. The easier version is to simply add async/await to your code:

async handleOnclickSelect(event) {
    let accountRec = event.detail.row;
    let targetRowId = accountRec.Id;
    await this.checkAccountDetails(targetRowId);
    console.log('Called On Click Method');
    console.log('Result Value ',this.hasresult);
}
async checkAccountDetails(accountId) {
    const returnValue = await checkAccountDtsRec({ accountId });
    // some logic
    this.hasresult = true;
    console.log('Logic Inside Method');
}

The await keyword makes the code wait until the Promise resolves. This is usually far easier to read and maintain than using Promise/then.

If you do want to use Promise/then, you still need to wait, which we can do like this:

handleOnclickSelect(event) {
    let accountRec = event.detail.row;
    let targetRowId = accountRec.Id;
    this.checkAccountDetails(targetRowId).then(() => {
    console.log("Called On Click Method");
    console.log("Result Value ", this.hasresult);
    });
}
checkAccountDetails(recordId) {
    return new Promise((resolve, reject) => {
    checkAccountDtsRec({
        accountId: recordId,
    }).then((result) => {
        if (result != undefined) {
        // some logic
        this.hasresult = true;
        console.log("Logic Inside Method");
        resolve(result);
        } else {
        reject(result);
        }
    });
    });
}
1
  • Understood . Thanks a lot @sfdcfox Commented Jan 5, 2023 at 17:45
0

Javascript is a synchronous single-threaded language but with the help of event-loop and promises, JavaScript is used to do asynchronous programming.

JS doesn't wait for responses from the apex, API, or promises and jumps to the next line of execution.

When the JS received the response it goes back and executes the response handler.

What you need to do is move these lines in the then block.

checkAccountDetails(recordId) {
    
            checkAccountDtsRec({
                accountId: recordId
            })
                .then(result => {
             if (result != undefined) {
                // some logic
                this.hasresult = true;
               console.log('Logic Inside Method');
               console.log('Called On Click Method');
               console.log('Result Value ',this.hasresult);
        }
    }

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.