我正试图围绕 bluebird Promises,并通过文档中的一些示例。我当前的代码基于此示例:
var Promise = require('bluebird');
var pg = Promise.promisifyAll(require('pg'));
var using = Promise.using;
function getConnection(string) {
var close;
return pg.connectAsync(string).spread(function(client, done) {
close = done;
return client;
}).disposer(function() {
console.log('In disposer');
try {
if (close) close();
} catch(e) {};
});
};
using(getConnection('/var/run/postgresql dc'), function(conn) {
console.log('Got a connection');
return conn.queryAsync('SELECT 1');
})
.then(function(rows) {
console.log('Retrieved %s row(s)',rows.rowCount);
});
输出如预期:
Got a connection
In disposer
Retrieved 1 row(s)
但是,程序永远不会终止。什么是挂断(双关语)?