Я хочу вызвать функцию, которая дважды возвращает обещание, но когда функция завершает вызов себя навсегда, иногда внешнюю, а иногда внутреннюю. Это проблема c функция, где retrieveDependency()
возвращает обещание.
function renderDiffProject(projName, branchName, res, org=undefined){
let newScan;
let oldScan;
retrieveDependency(projName, branchName, 1, org).then(result => {
newScan = result;
console.log("inside retrieve deps");
return retrieveDependency(projName, branchName, 2, org);
})
.then(result => {
oldScan = result;
console.log("successfully got dependencies")
res.render('project', {projname: projName, branch: branchName, proj:JSON.stringify(newScan), oldproj:JSON.stringify(oldScan)})
})
}
И это код для retrieveDependency()
function retrieveDependency(projname, branch, n, org=undefined){
return new Promise(function(resolve, reject){
depList = []
if (org != undefined){
var sql = "SELECT (scanid) FROM foozie WHERE project = '" + projname + "' AND branch = '" + branch + "' AND org = '" + org + "' ORDER BY hist_id DESC LIMIT " + (n-1) + ",1";
} else {
var sql = "SELECT (scanid) FROM foozie WHERE project = '" + projname + "' AND branch = '" + branch + "' ORDER BY hist_id DESC LIMIT " + (n-1) + ",1";
}
return database.query(sql).then(result => {
scan_id = result[0].scanid;
var sql = "SELECT * FROM branch_scan WHERE fossa_scan_id = '" + scan_id + "'";
return database.query(sql);
})
.then(result => {
var promiseList = [];
for (r in result){
dep_name_version = result[r].dep_name_version;
promiseList.push(getDepWrapper(dep_name_version, depList));
}
Promise.all(promiseList).then(result => {
console.log("pushed all the promises for number", n);
resolve(result);
})
})
})
}
И database.query
определяется как ниже:
class Database {
constructor() {
this.connection = mysql.createConnection({
host: 'azure.com',
user: 'admin-portal-mysql',
password: '2341234#1',
database: 'yatadb'
});
}
query( sql, args ) {
return new Promise( ( resolve, reject ) => {
this.connection.query( sql, args, ( err, rows ) => {
if ( err )
return reject( err );
resolve( rows );
} );
} );
}