It seems like a more comprehensive discussion of LWC async programming would be helpful. Luckily most stuff works if you follow the LWC examples, but once your design falls outside the examples, there isn't much to go on (with respect to async).
I had two situations where setting the active-section-name was unresponsive even though I was @tracking the variable I used to set the active-section-name.
Both issues were fixed by async/await Promise.resolve(), which was the answer in this question: (thank you @sfdcfox!)
Open multiple accordion section using active-section-name not working as expected
Here is the HTML for rendering a list of Task Notes:
...
<lightning-accordion allow-multiple-sections-open active-section-name={activeSections}>
<template iterator:it={notesViewModel}>
<lightning-accordion-section key={it.value.Id} name={it.value.Id}
label={it.value.LabelText}>
<div class="slds-box slds-theme_shade">
<lightning-formatted-rich-text value={it.value.BodyHtml}>
</lightning-formatted-rich-text>
</div>
</lightning-accordion-section>
</template>
</lightning-accordion>
...
Here is the wired function that gets a list of Task Notes: (see Promise.resolve())
...
@api recordId;
@track notesViewModel = [];
error;
@track activeSections = [];
@wire(getNotes, { parentId: '$recordId' })
async getNotesViewModel(result) {
if(result.data)
{
this.notesViewModelResult = result;
this.notesViewModel = this.getFilteredNotesViewModel(this.notesViewModelResult.data);
this.error = undefined;
await Promise.resolve();
this.activeSections = this.getInitialActiveSections(this.notesViewModel);
this.noteSummaryText = this.getNoteSummaryText();
}
else if (result.error)
{
this.notesViewModel = undefined;
this.error = result.error;
}
}
...
Here is a method used to filter the visible Task Notes: (see Promise.resolve())
async handleNoteTypeFilterChange(event)
{
this.noteTypeFilterValue = event.detail;
this.notesViewModel = this.getFilteredNotesViewModel(this.notesViewModelResult.data);
await Promise.resolve();
this.activeSections = this.getInitialActiveSections(this.notesViewModel);
}
The active-section-name attribute of the lightning-accordion component seems especially sensitive to timing. I tried the rendered and connected callbacks, but neither helped. To be clear, the code above works fine. I'm looking for documentation that clarifies why the additional promises are necessary/appropriate.
What are the clues (other than not working) that adding Promise.resolve() is the answer? (It feels like trial and error.)
Is there any documentation that would help? (I did not find any clues about using Promise.resolve() in this manner.)