Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forEach should await the iteratorCallback #154

Open
Iyashu5040 opened this issue Jun 5, 2018 · 3 comments
Open

forEach should await the iteratorCallback #154

Iyashu5040 opened this issue Jun 5, 2018 · 3 comments

Comments

@Iyashu5040
Copy link

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

@pipoa
Copy link

pipoa commented Aug 27, 2019

+1

@GFlores98
Copy link

Could you solve in any way? I have a similar problem

@mlynch
Copy link
Contributor

mlynch commented Mar 9, 2021

Will look into seeing if v3 addresses this (in a few weeks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants