As a beginner in posting on forums, I'm hoping this one goes smoothly. I've been working with promises for three hours now and still can't seem to grasp them. Unfortunately, Async Wait is not supported in our project and I'm currently using pouchdb over sqlite.
The issue I'm facing is with the display of recipes on my page. Initially, it returns empty and then lags behind by one recipe each time a new one is added. I suspect this is due to returning this.recipeBook before all promises are completed. Although in the code I shared, the final return statement is placed outside any promise, when I try putting it inside a promise, I encounter a "type void is not assignable to type any[]" error in RecipesPage.ts.
I would greatly appreciate any guidance or support you can provide in helping me understand the correct way to handle this situation. Thank you in advance!
This is the call in RecipesPage.ts:
this.recipes = this.database.getAllRecipes();
And here is the method in database.service.ts:
public getAllRecipes() {
this.recipeBook = new Array<Recipe>();
this.recipeDatabase.allDocs
({
include_docs: true,
}).then(docs => {
this.objectArray = docs.rows.map(row => {
return row.doc;
})
}).then(() => {
for (let object of this.objectArray) {
this.myRecipe = this.convertObjectToRecipe(object);
this.recipeBook.push(this.myRecipe);
}
})
return this.recipeBook;
}