两链异步函数内部和另一个循环内部的循环异步函数

时间:2018-11-08 10:20:13

标签: javascript typescript asynchronous ionic-framework

我有一个由loop1包装的3个异步函数,在成功执行前两个函数中的每一个后,前两个异步函数被链接在一起,最后一个异步函数被另一个循环包装。这将是问题所在,循环将不等待第三异步函数执行并返回值,然后再次循环。

将调用提供程序的ts代码,这是循环1

for(var i=0; i<maxValue; i++){
    if(this.loanSteps.length > i){
       this.localdb.insertStepsToApply(this.loanSteps[i]);
      }
    }

提供者功能代码

  insertStepsToApply(stepsValue){
    return this.sqlite.create({
      name: 'govservices.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      return db.executeSql('INSERT INTO application_steps(steps_program_refer_id,steps) VALUES ('+this.governmentprogram_id+',"'+stepsValue+'")',[])
      .then(res=>{
        console.log('inserted steps');   
        return db.executeSql('SELECT * FROM application_steps ORDER by appsteps_id DESC LIMIT 1', [])
        .then(async res=>{
          if(res.rows.length > 0){
              this.applyStepsid = res.rows.item(0).appsteps_id;
              console.log('extracting app steps id ->'+this.applyStepsid);
              var steplength = stepsValue.split(/\r\n|\r|\n/).length; // new line
              var stepLengthblankspace = (stepsValue.match(/^[ \t]*$/gm) || []).length; // blank spaces
              var numberOfSentences = steplength - stepLengthblankspace;

              for(var ix=0; ix < numberOfSentences; ix++){
                await db.executeSql('INSERT INTO requirement_list(requirement_government_program_id, requirement_steps_id) VALUES ('+this.governmentprogram_id+','+this.applyStepsid+')',[])
                .then(res =>{
                  alert('successfully inserted to steps apply requiermeent box');
                    return res;
                  }).catch(e =>{
                    console.log(e.message); 
                  });
              }
          }
        }).catch(e => console.log(e.message));
      }).catch(e => console.log(e.message));
    }).catch(e => console.log(e.message));
  }

请注意,内部循环将取决于steps值具有多少个句子,该stepvalue是一个文本框并具有一个段落 我想要的预期输出是这样的

inserted steps
extracting app steps id -> 3
successfully inserted to steps apply requirement box
successfully inserted to steps apply requirement box
successfully inserted to steps apply requirement box
inserted steps
extracting app steps id -> 4
successfully inserted to steps apply requirement box
successfully inserted to steps apply requirement box

但实际情况是这样

inserted steps
extracting app steps id -> 3
inserted steps
extracting app steps id -> 4
successfully inserted to steps apply requirement box
successfully inserted to steps apply requirement box
successfully inserted to steps apply requirement box
successfully inserted to steps apply requirement box
successfully inserted to steps apply requirement box

我也将promise放在内部循环(loop2)之外,但输出仍然不正确,关于如何处理类似内容的任何想法?

1 个答案:

答案 0 :(得分:0)

按照@Kokodoko的建议解决我的问题,创建类似此功能的另一个功能

    return db.executeSql('SELECT * FROM application_steps ORDER by appsteps_id DESC LIMIT 1', [])
    .then(async res=>{
      if(res.rows.length > 0){
          this.applyStepsid = res.rows.item(0).appsteps_id;
          console.log('extracting app steps id ->'+this.applyStepsid);
          var steplength = stepsValue.split(/\r\n|\r|\n/).length; // new line
          var stepLengthblankspace = (stepsValue.match(/^[ \t]*$/gm) || []).length; // blank spaces
          var numberOfSentences = steplength - stepLengthblankspace;
          console.log('total sentences '+numberOfSentences+' = '+steplength+' - '+stepLengthblankspace);
          for(var ix=0; ix < numberOfSentences; ix++){
            await this.insertIntoRequirementCheckbox();
          }
      }
    }).catch(e => console.log(e.message));

insertIntoRequirementCheckbox(){
return this.sqlite.create({
  name: 'govservices.db',
  location: 'default'
}).then((db: SQLiteObject) =>{
  return db.executeSql('INSERT INTO requirement_list(requirement_government_program_id, requirement_steps_id) VALUES ('+this.governmentprogram_id+','+this.applyStepsid+')',[])
  .then(res =>{
    console.log('requirement checkbox successfully inserted');
    }).catch(e => console.log(e.message));
  }).catch(e => console.log(e.message));
}