Here is my code that I'm trying to implement using node-postgres:
return pool.connect().then((client) => {
// if (err) {
// throw err;
// }
let updateTable = [];
console.log('ABOUT TO RUN QUERY');
// updateTable.executeQuery()
return client.query(sqlString, updateTable, (error, result) => {
console.log('RUNNING QUERY');
if (error) {
throw error;
}
console.log('RUNNING QUERY2');
// code
console.log('RUNNING QUERY3');
for (let i = 0; i < result.rows.length; i++) {
console.log('RUNNING QUERY4');
let row = result.rows[i];
// process data
}
console.log('RUNNING QUERY5');
// write to file
console.log('RUNNING QUERY6');
return client.release();
})
.then(() => {
console.log('CLIENT RELEASED');
if (!fileOnly) {
if (optionNode != null) {
console.log('ABOUT TO RUN QUERY #2');
// statsTable.executeQuery()
let statResults = client.query(withStatsString, statsTable, (err, res) => {
if (err) {
console.log(err);
return err;
}
return client.release();
});
//}
}
}
return pool.end();
})
.then(() => {
return reportObject;
})
.catch(e => {
throw e;
});
})
.catch((e) => {
console.log(e);
return reportObject;
});
When I run this code, I can see:
RUNNING QUERY
RUNNING QUERY2
RUNNING QUERY3
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY5
RUNNING QUERY6
However, it never reaches the then
where the client is released. I will print out literally right before ending the Promise
, but will hang infinitely and never resolve. How do I fix my Promise chain?
EDIT: I was able to fix the client.query
, but in the index.js
, my program is hanging on completion. This is the code:
ReportUtil.sendInReport('Monthly_2017_01', JSON.parse(reportRequest), 100, null)
.then(result => {
console.log(result.status);
console.log(result.header);
console.log(result.data);
return Promise.resolve();
}).finally(() => {});
After this code, it just hangs and the program never ends. How do I escape this Promise chain?
As per the docs on the release callback:
You must call the releaseCallback or client.release (which points to the releaseCallback) when you are finished with a client. If you forget to release the client then your application will quickly exhaust available, idle clients in the pool and all further calls to pool.connect will timeout with an error or hang indefinitely if you have connectionTimeoutMills configured to 0.
Try not returning the client.release
(like it is a promise - which it is not) but just calling it lower in the promise chain like this:
return pool.connect().then((client) => {
let updateTable = [];
console.log('ABOUT TO RUN QUERY');
// updateTable.executeQuery()
return client.query(sqlString, updateTable)
.then(result => {
console.log('RUNNING QUERY');
console.log('RUNNING QUERY2');
// code
console.log('RUNNING QUERY3');
for (let i = 0; i < result.rows.length; i++) {
console.log('RUNNING QUERY4');
let row = result.rows[i];
// process data
}
console.log('RUNNING QUERY5');
// write to file
console.log('RUNNING QUERY6');
})
.then(() => {
if (!fileOnly) {
if (optionNode != null) {
console.log('ABOUT TO RUN QUERY #2');
// statsTable.executeQuery()
let statResults = client.query(withStatsString, statsTable)
.then(res => {
});
}
}
client.release();
console.log('CLIENT RELEASED');
return pool.end();
})
.then(() => {
return reportObject;
})
.catch(e => {
client.release();
throw e;
});
})
.catch((e) => {
console.log(e);
return reportObject;
});
And for exiting your index.js
using a Promise chain:
ReportUtil.sendInReport('Monthly_2017_01', JSON.parse(reportRequest), 100, null)
.then(result => {
console.log(result.status);
console.log(result.header);
console.log(result.data);
})
.catch(e => {
console.log(e);
})
.finally(() => {
process.exit();
});