You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.8 KiB

7 years ago
  1. # on-finished
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Execute a callback when a request closes, finishes, or errors.
  8. ## Install
  9. ```sh
  10. $ npm install on-finished
  11. ```
  12. ## API
  13. ```js
  14. var onFinished = require('on-finished')
  15. ```
  16. ### onFinished(res, listener)
  17. Attach a listener to listen for the response to finish. The listener will
  18. be invoked only once when the response finished. If the response finished
  19. to to an error, the first argument will contain the error.
  20. Listening to the end of a response would be used to close things associated
  21. with the response, like open files.
  22. ```js
  23. onFinished(res, function (err) {
  24. // clean up open fds, etc.
  25. })
  26. ```
  27. ### onFinished(req, listener)
  28. Attach a listener to listen for the request to finish. The listener will
  29. be invoked only once when the request finished. If the request finished
  30. to to an error, the first argument will contain the error.
  31. Listening to the end of a request would be used to know when to continue
  32. after reading the data.
  33. ```js
  34. var data = ''
  35. req.setEncoding('utf8')
  36. res.on('data', function (str) {
  37. data += str
  38. })
  39. onFinished(req, function (err) {
  40. // data is read unless there is err
  41. })
  42. ```
  43. ### onFinished.isFinished(res)
  44. Determine if `res` is already finished. This would be useful to check and
  45. not even start certain operations if the response has already finished.
  46. ### onFinished.isFinished(req)
  47. Determine if `req` is already finished. This would be useful to check and
  48. not even start certain operations if the request has already finished.
  49. ### Example
  50. The following code ensures that file descriptors are always closed
  51. once the response finishes.
  52. ```js
  53. var destroy = require('destroy')
  54. var http = require('http')
  55. var onFinished = require('on-finished')
  56. http.createServer(function onRequest(req, res) {
  57. var stream = fs.createReadStream('package.json')
  58. stream.pipe(res)
  59. onFinished(res, function (err) {
  60. destroy(stream)
  61. })
  62. })
  63. ```
  64. ## License
  65. [MIT](LICENSE)
  66. [npm-image]: https://img.shields.io/npm/v/on-finished.svg?style=flat
  67. [npm-url]: https://npmjs.org/package/on-finished
  68. [node-version-image]: https://img.shields.io/node/v/on-finished.svg?style=flat
  69. [node-version-url]: http://nodejs.org/download/
  70. [travis-image]: https://img.shields.io/travis/jshttp/on-finished.svg?style=flat
  71. [travis-url]: https://travis-ci.org/jshttp/on-finished
  72. [coveralls-image]: https://img.shields.io/coveralls/jshttp/on-finished.svg?style=flat
  73. [coveralls-url]: https://coveralls.io/r/jshttp/on-finished?branch=master
  74. [downloads-image]: https://img.shields.io/npm/dm/on-finished.svg?style=flat
  75. [downloads-url]: https://npmjs.org/package/on-finished