当我尝试进行一些数据库搜索时,我发现了两种代码可以做到这一点。但我不知道哪种更喜欢以及为什么..
// a.js
export default oracledb.createPool(configuration)
第一种方式(看起来效果很好,但不符合承诺规范):
// b.js
import main from a.js;
main.then((pool)=>{
pool.getConnection().then((connection)=>{
connection.execute(sql).then((result)=>{
console.log(result);
connection.close();
}).catch(err=>{
if(connection){connection.close()}
})
});
})
这是第二种方式:
let connection;
main.then((pool)=>{
return pool.getConnection()
}).then((connection)=>{
return connection.execute(sql)
}).then((result)=>{
console.log(result);
connection.close();
}).catch(err=>{
if (connection){connection.close()}
});
这个问题可能不仅仅是关于数据库操作,而是组织承诺链的正确方法。有人可以帮我吗?