Quantcast
Channel: Imperative Call Using Await In LWC - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 3

Imperative Call Using Await In LWC

$
0
0

Is it possible to imperatively call a function from a wired function using async/await? And yes, I've seen similar questions - but they kinda miss the point I'm trying to make.

Consider this simple example:

@wire(getRecord, { recordId: '$recordId', fields: RECORD_FIELDS })wiredRecord({ error, data }) {    if (error) {        console.log(error);    } else if (data) {        fetchMoreData({ recordId: data.fields.Id.value })            .then((data) => {                doStuff();            })            .catch((error) => {                console.log(error);            });    }}

I find this hard to read and more error-prone compared to the version using await:

@wire(getRecord, { recordId: '$recordId', fields: RECORD_FIELDS })async wiredRecord({ error, data }) {    if (error) {        console.log(error);    } else if (data) {        try {            const moreData = await fetchMoreData({ recordId: data.fields.Id.value });            doStuff();        } catch (error) {            console.log(error);        }    }}

But the second version needs async to be added to wiredRecord which a) doesn't really do anything because @wire makes this already async by design and b) nobody uses it. But it gets even worse if you stitch multiple calls together - it makes it even harder to read.

Is this the only way of accomplishing this? And if it is - is there a downside to it?


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>