据我了解,我尝试过重构以下代码。
- 您认为我应该兑现承诺吗?如果我们继续使用第一个响应的结果,可以吗?
重构后
const sql = require('mssql');
const connectionString = 'somecnnstring';
const query1 = 'some-query-1';
const query2 = 'some-query-2';
const query3 = 'some-query-3';
function getAccountId() {
sql.connect(connectionString).then(() => {
return sql.query(query1)
}).then(result1 => {
console.log(result1)
return sql.query(query2, {...result1})
}).then(result2 => {
console.log(result2)
return sql.query(query3, {...result2})
}).then(result3 => {
console.log(result3)
}).catch(err => {
console.log(err)
})
}
module.exports = {
getAccountId,
};
之前
const sql = require('mssql');
const connectionString = 'some-cnn-string';
const query1 = 'some-query-1';
const query2 = 'some-query-2';
const query3 = 'some-query-3';
module.exports = {
getAccountId: function(apiKey, cb) { sql.connect(connectionString, (connection)=>{
connection.query(query1, (result)=>{
console.log(result1);
connection.query(query2, { ...result1 }, (result)=>{
console.log(result2);
connection.query(query3, { ...result2 },
console.log(result3);
})
}) });
}) }
};