Assume I have an LWC component and Apex Aura controller:
/* AccountController.cls */
public with sharing class AccountController {
@AuraEnabled
public static List<Account> getAll() {
try {
/* some foo logic */
return [SELECT Id, Name FROM Account];
} catch (Exception e) {
throw new AuraHandledException(e.getMessage());
}
}
}
<!--cmp.html --->
<template>
<template if:false={isLoaded}>
<lightning-spinner variant="brand" alternative-text="Loading..."></lightning-spinner>
</template>
</template>
/* cmp.js */
import { LightningElement, track } from 'lwc';
import getAll from '@salesforce/apex/AccountController.getAll';
export default class Cmp extends LightningElement {
@track isLoaded = false;
@track accounts;
@track error;
connectedCallback() {
getAll()
.then(result => {
console.log('1st then');
this.accounts = result;
/* or any other function that returns promise */
return this.promiseFunc();
})
.then(value => {
console.log('2nd then executes after 3 seconds , value: ' + value);
})
.catch(error => {
this.error = error;
})
.finally(() => {
this.isLoaded = true;
});
}
promiseFunc() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 3000);
});
}
}
This example works perfectly, but when tried to rewrite promises into async/await:
/* cmp.js */
import { LightningElement, track } from 'lwc';
import getAll from '@salesforce/apex/AccountController.getAll';
export default class Cmp extends LightningElement {
@track isLoaded = false;
@track accounts;
@track error;
connectedCallback() {
var result = await getAll();
this.accounts = result;
var value = await this.promiseFunc();
console.log('returned after 3s , value: ' + value);
this.isLoaded = true;
}
}
It throws Error
Error:(35, 15) LWC1503: Parsing error: await is a reserved word (35:15)
But if both getAll()
and this.promiseFunc()
returns promises, why can't I await them?
Is async/await
syntax is allowed for Lightning Web Components?
If yes then how to rewrite promises into async/await
correctly and how to catch possible errors?