0

Below is the JS file.

async tcvChange(event) {
        
        let tempList = JSON.parse(JSON.stringify(this.lineItems));

        console.log('list before ++'+JSON.stringify(tempList));
        
        await tempList.map(async e => {
            if (e.productId === event.target.name) {
                e.productTerm = event.target.value;

                await getTCV({productTerm: e.productTerm})
                .then(result =>{
                    e.tcv = result;                    
                })

                console.log('tcv ++'+e.tcv);    
            }
        })
        
        console.log('updated list ++'+JSON.stringify(tempList));
        this.lineItems = tempList;
        
    }

Console Output -->

list before ++
updated list ++
tcv ++

Expected Output

list before ++
tcv ++
updated list ++
2
  • as per my understanding the map await also shall be promise. Then it works smooth. – Ysr Shk Oct 14 '20 at 15:19
  • Are you calling apex method getTCV here? Why not to send the list of productTerm and have a single apex call? – Rahul Gawale Oct 14 '20 at 15:36
1
  1. You should have either await or then..catch.

  2. await will wait for the server trip before continuing the main transaction but then..catch functions will be passed to async thread for processing later after main transaction is finished.

You need to implement as below:

----- code ---------
        tempList.forEach(async e => {
            if (e.productId === event.target.name) {
                e.productTerm = event.target.value;

                const result = await getTCV({productTerm: e.productTerm});
                e.tcv = result;                    

                console.log('tcv ++'+e.tcv);    
            }
        })
----- code -------

However, this is not efficient. I would recommend to bulkify getTCV to accept list of product terms - List<String>. Since you are also using event.target.value, I presume you are invoking this function for some input change. So, I am not sure why you want to change all the lineItems. Anyway, you need to rethink the design for this function to make it efficient.

1
  • I tried implementing above code but it still not working. tcv ++ is coming at end in console logs – arpit gupta Oct 15 '20 at 5:04
0

I found the solution for the issue. Here we need to use Promise.all to get the result from all promises and then proceeds to next statement. :-


        const promises = tempList.map(async e => {
            if (e.productId === event.target.name) {
                e.productTerm = event.target.value;

                await getTCV({productTerm: e.productTerm})
                .then(result =>{
                    e.tcv = result;                    
                })

                console.log('tcv ++'+e.tcv);    
            }
        })
        await Promise.all(promises);
        console.log('updated list ++'+JSON.stringify(tempList));
        this.lineItems = tempList;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.