Basic overview.
I have a data set i want to loop through with a for statement.
i dont want to proceed to the next item in the for loop until both functions inside complete. function 1 is dependant on function 2 suceeding, if either fails i would like the loop to stop.
here are my two functions.
submitEntryPromise(entry): Promise < any > {
return new Promise((resolve, reject) => {
this.submitEntry(entry).take(1).subscribe(data => {
if (data) {
resolve(data);
}
}, (error: any) => {
console.log('rejected submitEntry' + JSON.stringify(error));
reject(false);
});
});
}
deleteEntryFromSqlLitePromise(rowid): Promise < any > {
return new Promise((resolve, reject) => {
this.deleteEntry(rowid).then(res => {
if (res) {
resolve(res);
} else {
reject(false);
}
}).catch((error: any) => {
console.log('reject delete' + JSON.stringify(error));
reject(false);
});
});
}
My data set (returned from sql lite local db) is named savedEntries
I was trying something like this.
for (let i = 0, p = Promise.resolve(); i < savedEntries.rows.length; i++) {
p = p.then(_ => this.wpservice.submitEntryPromise(
{
score: savedEntries.rows.item(i).score,
email: savedEntries.rows.item(i).email
}))
}
but i am not sure where to call my deleteEntryFromSqlLitePromise function and i am not sure if these will execute in order?
essentially im looping through saved entries in an offline sql db, and syncing them to my server, when the server says it received an entry i want to remove it from the local db.
any advice would be appreciated, im writing in typescript for my ionic project.