twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

README.md (2616B)


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