0

for loop not waiting for reponse of getRoleName function. how to do this?Issue with for loop and promise.

for(var i = 0; i < data.rows.length; i++) {
  alert("inside" + i);
  this.getRoleName(data.rows.item(i).role_id).then((data1)=>{
    this.users.push({
      id: data.rows.item(i).id,
      text: data.rows.item(i).username,
      role: data1.rows.item(i).role_text
    });
  },(error)=>{
    });
}

getRoleName(id):Promise<any> {
  let text : String;
  let sql = "select role_text from roles where id = "+id;
  return this.db.executeSql(sql, {}).then((data) => {
    return data;
  },(error) => { alert("i im in eerror");
    console.error("Unable to execute sql", JSON.stringify(error));
  });
}
  • getRoleName(id):Promise { alert("calllllledddddddd"); let text : String; let sql = "select role_text from roles where id = "+id; return this.db.executeSql(sql, {}).then((data) => { //this.roleName = data.rows.item(0).role_text; //alert(this.roleName); return data; }, (error) => { alert("i im in eerror"); console.error("Unable to execute sql", JSON.stringify(error)); } ); } – Nitin Bhatia Jan 18 '17 at 15:51
  • 1
    Please edit your original post to include that extra code. Putting code in a comment makes it very difficult to read. – Michael Dodd Jan 18 '17 at 15:57
  • 1
    Possible duplicate of [Asynchronous Process inside a javascript for loop](http://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop) – Suraj Rao Jan 19 '17 at 05:54
  • done @MichaelDodd , please have a look it again. – Nitin Bhatia Jan 19 '17 at 08:16

0 Answers0