2

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.

  • You should look at `async/await` it makes code like this easier to read and write. – Titian Cernicova-Dragomir Mar 14 '18 at 20:42
  • Duannx, pretty similar, my main struggle was making sure both items fired before going to the next part of the loop, i got somewhere using promise chains, but im hoping to use async/await like Titian suggested, seems to be much cleaner looking code. will be posting what i discovered shorty. – do a little better each time Mar 15 '18 at 15:59

1 Answers1

0

Since you are using Typescript you should consider async/await it allows you to write async code in a sync looking way:

async deleteEntryFromSqlLitePromise(rowid): Promise<any> {
    try {
        let res = await this.deleteEntry(rowid);
        return res || false;
    } catch (error) {
        console.log('reject delete' + JSON.stringify(error));
        return false;
    }
}

async deleteAll(savedEntries: { rows: { length: number, item(i: number): any } }) {
    let rows = savedEntries.rows;
    for (let i = 0; i < rows.length; i++) {
        try {
            let synced = await this.wpservice.submitEntryPromise(
                {
                    score: rows.item(i).score,
                    email: rows.item(i).email
                });
            if (synced) {
                let deleted = await this.wpservice.deleteEntryFromSqlLitePromise(rows.item(i).rowid)
                if (!deleted) {
                    // decide whta to do if not deleted
                }
            } else {
                // decide what to do if not synced
            }
        } catch (e) {
            // Decide what to do with any errors
        }
    }
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357