ionic-team / ionic-team/ionic-storage
forEach should await the iteratorCallback
- Vorherrschende Sprache
- TypeScript
- Sterne
- 456
- Forks
- 97
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
We have a situation where we want to clear a bunch of items from storage, and then set items immediately after. We need to await the clear step before saving to prevent the clear from clearing the newly added items.
We are using `forEach` to iterate through storage, check each item and `remove` it if need be. Since `remove` is async we are awaiting it in the `iteratorCallback` and our `iteratorCallback` is marked as async. From the source code for storage we can see that the iteratorCallback is not awaited. This means that the `forEach` Promise could resolve before all the `remove`s have completed.
Here is a sample of what our `clear` function is doing:
```
async clear() {
await this.storage.forEach(async (v, k, i) => {
if (k.startsWith(this.barcodeStorageKey) || k.startsWith(this.principleStorageKey)) {
await this.storage.remove(k);
}
});
}
```
We are using ` "@ionic/storage": "2.1.3"`
Beitragsleitfaden
Rechercherichtung
Start at the storage forEach implementation and trace how it invokes iteratorCallback. Confirm that the returned Promise waits for async remove calls to finish, so a subsequent save cannot race with clear; the payload names no specific file or test.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- databases
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100