0.8.2 • Published 8 years ago

after v0.8.2

Weekly downloads
4,255,152
License
MIT
Repository
github
Last release
8 years ago

After Build Status

Invoke callback after n calls

Status: production ready

Example

var after = require("after")
var db = require("./db") // some db.

var updateUser = function (req, res) {
  // use after to run two tasks in parallel,
  // namely get request body and get session
  // then run updateUser with the results
  var next = after(2, updateUser)
  var results = {}
  
  getJSONBody(req, res, function (err, body) {
    if (err) return next(err)
    
    results.body = body
    next(null, results)
  })
  
  getSessionUser(req, res, function (err, user) {
    if (err) return next(err)
    
    results.user = user
    next(null, results)
  })
  
  // now do the thing!
  function updateUser(err, result) {
    if (err) {
      res.statusCode = 500
      return res.end("Unexpected Error")
    }
    
    if (!result.user || result.user.role !== "admin") {
      res.statusCode = 403
      return res.end("Permission Denied")
    }
    
    db.put("users:" + req.params.userId, result.body, function (err) {
      if (err) {
        res.statusCode = 500
        return res.end("Unexpected Error")
      }
      
      res.statusCode = 200
      res.end("Ok")  
    })   
  }
}

Naive Example

var after = require("after")
    , next = after(3, logItWorks)

next()
next()
next() // it works

function logItWorks() {
    console.log("it works!")
}

Example with error handling

var after = require("after")
    , next = after(3, logError)

next()
next(new Error("oops")) // logs oops
next() // does nothing

// This callback is only called once.
// If there is an error the callback gets called immediately
// this avoids the situation where errors get lost.
function logError(err) {
    console.log(err)
}

Installation

npm install after

Tests

npm test

Contributors

  • Raynos
  • defunctzombie

MIT Licenced

@planetary-ssb/pub@httptoolkit/win-detect-browsers@arisageha/react-lazyload@arisageha/react-lazyload-fixeni-chat@everything-registry/sub-chunk-1104taterswin-detect-browsersweb-component-tester-bundlevx-teste-mark3vue-infinite-loading-propsworkshopper-boilerplateworkshopper-exerciseworkshopper-exercise-huworkshopper-recorderworkshopper-wrappedexecworkshopper-adventureworkshopper-adventure-adaptedworkshopper-adventure-huworkshopexamplesbot-test-guardriddlet-serverstoremansplinkstash-changelogrelay-servertestdependenciesmoduleswitch-writablethmbvcloudcam-playkit-js-hlsvideo-multi-uploadervue-dev-clone3.0.0miguelcostero-ng2-toastymkprojmemvaz.routermemvaz.utilslevel-atomic-counterlevel-scoutlevel-microbloglevel-multiplylevel-snapshotlevel-pathwiselibkvless-is-morelearn-sasslearnreactlearnyounodelearnyounode-hulearnyounode-jplearnyounode-zh-cnlearnyounode-zh-twlearnyounode_1and1learnyoupythongjallarhorngoeasy.engine-parsergoingnativejeuxuihydrosimage-cropperintrotowebglinjectolevel-transactionlevel-ttllevelmeup-jpmpd-parser-1morkdownhtml-chunk-processi18n-it-allkappa-bridgekappa-wrapperka-flow.jsjavascriptworkshopghreleasesglint-i18nglob-cpglob-lnncoremultipackmulti-tapmultileveldown-hangmy-changelogmd-socket-servermedea-compressedmedia-packagernode-version-datanodejs-dist-indexernodejs-nightly-builderpr-metadatapubnub-streampolyfill-middlewareplaykit-js-hls-sondqpotluckng-search-dropdownmupkgreact-native-engine.io-parserreact-redux-demo1new-contributorsnpm-republicatenpm-maintainers
0.8.2

8 years ago

0.8.1

11 years ago

0.7.0

12 years ago

0.6.0

12 years ago

0.5.0

12 years ago

0.4.1

13 years ago

0.4.0

13 years ago

0.3.4

13 years ago

0.3.3

13 years ago

0.3.2

13 years ago

0.3.1

13 years ago

0.2.0

13 years ago

0.1.6

13 years ago

0.1.5

13 years ago

0.1.4

13 years ago

0.1.3

13 years ago

0.1.0

13 years ago

0.1.8

13 years ago

0.1.7

13 years ago