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.

115 lines
2.6 KiB

8 years ago
  1. # After [![Build Status][1]][2]
  2. Invoke callback after n calls
  3. ## Status: production ready
  4. ## Example
  5. ```js
  6. var after = require("after")
  7. var db = require("./db") // some db.
  8. var updateUser = function (req, res) {
  9. // use after to run two tasks in parallel,
  10. // namely get request body and get session
  11. // then run updateUser with the results
  12. var next = after(2, updateUser)
  13. var results = {}
  14. getJSONBody(req, res, function (err, body) {
  15. if (err) return next(err)
  16. results.body = body
  17. next(null, results)
  18. })
  19. getSessionUser(req, res, function (err, user) {
  20. if (err) return next(err)
  21. results.user = user
  22. next(null, results)
  23. })
  24. // now do the thing!
  25. function updateUser(err, result) {
  26. if (err) {
  27. res.statusCode = 500
  28. return res.end("Unexpected Error")
  29. }
  30. if (!result.user || result.user.role !== "admin") {
  31. res.statusCode = 403
  32. return res.end("Permission Denied")
  33. }
  34. db.put("users:" + req.params.userId, result.body, function (err) {
  35. if (err) {
  36. res.statusCode = 500
  37. return res.end("Unexpected Error")
  38. }
  39. res.statusCode = 200
  40. res.end("Ok")
  41. })
  42. }
  43. }
  44. ```
  45. ## Naive Example
  46. ```js
  47. var after = require("after")
  48. , next = after(3, logItWorks)
  49. next()
  50. next()
  51. next() // it works
  52. function logItWorks() {
  53. console.log("it works!")
  54. }
  55. ```
  56. ## Example with error handling
  57. ```js
  58. var after = require("after")
  59. , next = after(3, logError)
  60. next()
  61. next(new Error("oops")) // logs oops
  62. next() // does nothing
  63. // This callback is only called once.
  64. // If there is an error the callback gets called immediately
  65. // this avoids the situation where errors get lost.
  66. function logError(err) {
  67. console.log(err)
  68. }
  69. ```
  70. ## Installation
  71. `npm install after`
  72. ## Tests
  73. `npm test`
  74. ## Contributors
  75. - Raynos
  76. - defunctzombie
  77. ## MIT Licenced
  78. [1]: https://secure.travis-ci.org/Raynos/after.png
  79. [2]: http://travis-ci.org/Raynos/after
  80. [3]: http://raynos.org/blog/2/Flow-control-in-node.js
  81. [4]: http://stackoverflow.com/questions/6852059/determining-the-end-of-asynchronous-operations-javascript/6852307#6852307
  82. [5]: http://stackoverflow.com/questions/6869872/in-javascript-what-are-best-practices-for-executing-multiple-asynchronous-functi/6870031#6870031
  83. [6]: http://stackoverflow.com/questions/6864397/javascript-performance-long-running-tasks/6889419#6889419
  84. [7]: http://stackoverflow.com/questions/6597493/synchronous-database-queries-with-node-js/6620091#6620091
  85. [8]: http://github.com/Raynos/iterators
  86. [9]: http://github.com/Raynos/composite