node.js - How can I release a resource in a nodeJS Q promise-chain and return a promise? -
new promises & q.
i call method abstracts underlying resource. method open resource processing close resource.
something this:
module.exports = function fetchrecords() { return openconnection() .then(fetchrecords) .then(groupbyid) .catch(rethrow) .done(closeconnection); } function closeconnection(result) { releaseconnection(); return result; } since i'm calling done return not promise undefined.
i hoping in client do:
resource.fetchrecords().then(/*do else*/); it looks have expose underlying resource client can do:
resource .openconnection() .then(resource.fetchrecords) .then(/*do else*/) .catch(/*..*/) .end(resource.close) i don't know q or promises...but thought maybe there better way?
should do:
module.exports = function fetchrecords() { return openconnection() .then(fetchrecords) .then(groupbyid) .then(closeconnection) .catch(rethrow); }
instead of done use finally, returns promise:
https://github.com/kriskowal/q/wiki/api-reference#promisefinallycallback
module.exports = function fetchrecords() { return openconnection() .then(fetchrecords) .then(groupbyid) .catch(rethrow) .finally(closeconnection); }
Comments
Post a Comment