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 (as the framework calls it when needed) 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?