1

I am trying to use async /await in an LWC component as stated THIS LINK

But while deploying my code i am getting LWC1001: Unexpected compilation error: Cannot read property 'type' of null

I have done both the ways 1)Declare some async init() function and invoke it within connectedCallback() hook: 2)Invoke it anonymously as an IIFE:

APEX

public class myAsyncAwaitController(){ 
 @AuraEnabled
    public static Integer methodName1(){
        try {
            return 1;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }    
    @AuraEnabled
    public static Integer methodName4(Integer i){
        try {
            return 5+i;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
}
  1. Declare some async init() function and invoke it within connectedCallback() hook:

     import { LightningElement} from 'lwc';
     import methodName1 from '@salesforce/apex/myAsyncAwaitController.methodName1';
     import methodName4 from '@salesforce/apex/myAsyncAwaitController.methodName2';
    
     export default class AsycAwaitLWC extends LightningElement {
         connectedCallback(){  
            this.initfunc();
         }
         async initfunc(){
             try{
                 console.log('Async Called Invoked');
                 let result1 =  await methodName1();
                 let result2 =  await methodName4(result1);
                 let finalResult = result1+result2; 
                 console.log('finalResult',finalResult);
             }
             catch{
                 console.log('Inside Catch');
             }
             finally{
                 console.log('Finally !!!');
             }
         }
     }
    

Option 2 : As an IIFE

import { LightningElement} from 'lwc';
import methodName1 from '@salesforce/apex/myAsyncAwaitController.methodName1';
import methodName4 from '@salesforce/apex/myAsyncAwaitController.methodName2';


export default class AsycAwaitLWC extends LightningElement {
    connectedCallback(){  
      (async()=>{
        try{
            console.log('Async Called Invoked');
            let result1 =  await methodName1();
            let result2 =  await methodName4(result1);
            let finalResult = result1+result2; 
            console.log('finalResult',finalResult);
        }
        catch{
            console.log('Inside Catch');
        }
        finally{
            console.log('Finally !!!');
        }
      })()
    }
}   

2 Answers 2

1
import methodName4 from '@salesforce/apex/myAsyncAwaitController.methodName2';

Should apparently be:

import methodName4 from '@salesforce/apex/myAsyncAwaitController.methodName4';

I'm not sure why you're getting such an odd error method, but this is the only thing I can see that stands out immediately.

Either calling approach should work.

3
  • Oh yes that was the typo.. but its not working even for my manager , wondering if it is some VS code config issue , but i am not sure Commented Mar 13, 2021 at 4:59
  • @SwarandeepSingh What does "not working" mean? That's not very descriptive.
    – sfdcfox
    Commented Mar 13, 2021 at 5:41
  • I mean he is also getting same error and unable to deploy Commented Mar 13, 2021 at 5:48
1

Please correct the imports as per @sfdcfox.

Also, make sure to correct catch block as "catch(error)". Please refer to below code:

    export default class AsycAwaitLWC extends LightningElement {
    connectedCallback(){  
        this.initfunc();
     }
     async initfunc(){
         try{
             console.log('Async Called Invoked');
             let result1 =  await methodName1();
             let result2 =  await methodName4(result1);
             let finalResult = result1+result2; 
             console.log('finalResult',finalResult);
         }
         catch(error){
             console.log('Inside Catch');
         }
         finally{
             console.log('Finally !!!');
         }
     }
}

Hope this resolves your issue.

You must log in to answer this question.

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