私は約束.then
を使用しています。このチェーンでは、いくつかの境界を計算し、SQL文を作成してデータベースに要求を送信します。データベースリクエストで結果が得られない場合は、境界を計算して何かを変更し、同じ手順を再度実行します。私はデータベースの結果があるまでこれを繰り返す。Node.jsデータベースリクエストを.thenチェーンで返します
これは私のコードです:
.then(function(){
return calcBound.calcBounds(req.body,0);
})
.then(function(options){
return sqlStatementBuilder.sqlStatementBuilder(options);
})
.then(function(statement){
return db_request.db_request(statement);
})
.then(function(dbResult){
if(dbResult.length <= 0){ // if there are no results from the database
console.log("There are no results for this filter options");
var newDBResult;
do{
newDBResult = calcBound.calcBounds(req.body, addToOffset)
.then(function(options){
return sqlStatementBuilder.sqlStatementBuilder(options);
})
.then(function(statement){
return db_request.db_request(statement);
})
} while(dbResult.length <= 0);
return newDBResult.sort(sortArray.compareRecordId);
}else{
return dbResult.sort(sortArray.compareRecordId);
}
})
whileループは彼女が、これは「メモリのうち、ヒープ」となってしまいます良いアイデアではありません。
これを行うには、もっと良い解決策はありますか?
で結果を得るまでないwhileループは、あなたが 'max_tries'が...それ以外の場合は、なぜあなたの関数が入っている必要がある場合はそれを呼び出しますループでメモリが不足していますか?何かが間違っているかもしれません... –
関数を再帰的に呼び出します。 – yBrodsky
変数のパラメータとして 'addToOffset'を使用して再帰関数を呼び出すと、このシナリオでは解決策になります。ループ内で – amanpurohit