3つの非同期関数がloop1でラップされています。最初の2つの非同期関数は、最初の2つの関数のそれぞれが正常に実行された後にチェーンされ、最後の非同期関数は別のループでラップされます。これが問題になります。ループは、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
私はまた、内側のループ(loop2)の外側にpromiseを置きますが、それでも出力は正しくありません。このようなものを処理する方法について何か考えはありますか?
@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));
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加