Node.js 在 .then 链中重复数据库请求
Node.js repeat database request in .then chain
我使用 promise .then
链。在这个链中,我计算了一些边界,构建 sql 语句并向数据库发送请求。如果数据库请求没有结果,我想通过计算边界来改变一些东西,然后再次执行相同的步骤。我想重复这个直到有数据库结果。
这是我的代码:
.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 循环不是一个好主意,她最终会变成 "heap out of memory"。
什么是更好的解决方案?
创建一个函数 dummyRecursiveFunction
并以 addToOffset
作为参数并调用它直到在 dbResult
中得到结果
function dummyRecursiveFunction(addToOffset) {
someFunction()
.then(function(){
return calcBound.calcBounds(req.body, addToOffset);
})
.then(function(options){
return sqlStatementBuilder.sqlStatementBuilder(options);
})
.then(function(statement){
return db_request.db_request(statement);
})
.then(function(dbResult) {
if(dbResult.length > 0) {
return dbResult.sort(sortArray.compareRecordId);
} else {
// newOffset: Your recalculated offset value.
dummyRecursiveFunction(newOffset);
}
});
}
我使用 promise .then
链。在这个链中,我计算了一些边界,构建 sql 语句并向数据库发送请求。如果数据库请求没有结果,我想通过计算边界来改变一些东西,然后再次执行相同的步骤。我想重复这个直到有数据库结果。
这是我的代码:
.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 循环不是一个好主意,她最终会变成 "heap out of memory"。
什么是更好的解决方案?
创建一个函数 dummyRecursiveFunction
并以 addToOffset
作为参数并调用它直到在 dbResult
function dummyRecursiveFunction(addToOffset) {
someFunction()
.then(function(){
return calcBound.calcBounds(req.body, addToOffset);
})
.then(function(options){
return sqlStatementBuilder.sqlStatementBuilder(options);
})
.then(function(statement){
return db_request.db_request(statement);
})
.then(function(dbResult) {
if(dbResult.length > 0) {
return dbResult.sort(sortArray.compareRecordId);
} else {
// newOffset: Your recalculated offset value.
dummyRecursiveFunction(newOffset);
}
});
}