带有承诺的文档是可怕的。连接数据库句柄并运行类似快速路由的正确方法是什么?
var Promise = require('bluebird');
var db2 = Promise.promisifyAll(fb);
var dbh = db2.connectAsync({
host: '127.0.0.1',
database: 'CAFW.FDB',
user: 'SYSDBA',
password: 'pw'
}
);
所以现在我有了 dbh,它是一个Promise
. 我在我的路线中做什么......
app.get('stuff' function () {
// How do I use it here?
});
app.get('otherstuff' function () {
// How do I use it here?
});
做类似的事情的正确方法是......
var db2 = Promise.promisifyAll(fb);
dbh.then( function (dbh) {
// This is asyncronous code, Express doesn't use promises
app.get('stuff', function () {
// And, here I have DBH -- but this is pyramid code again.
// Do I avoid this pattern? Or, is this required
};
app.get('otherstuff', function () {
// dbh here.
};
} );
因为如果是这样,那实际上