ionic-team / ionic-team/ionic-storage

forEach should await the iteratorCallback

Open
#154 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
456
Forks
97
PR merge metrics
No merged PRs in 30d

Description

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"`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.