after

WebJar for after

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

after
Last Version

Last Version

0.8.2
Release Date

Release Date

Type

Type

jar
Description

Description

after
WebJar for after
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/Raynos/after

Download after

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/after/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>after</artifactId>
    <version>0.8.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/after/
implementation 'org.webjars.npm:after:0.8.2'
// https://jarcasting.com/artifacts/org.webjars.npm/after/
implementation ("org.webjars.npm:after:0.8.2")
'org.webjars.npm:after:jar:0.8.2'
<dependency org="org.webjars.npm" name="after" rev="0.8.2">
  <artifact name="after" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='after', version='0.8.2')
)
libraryDependencies += "org.webjars.npm" % "after" % "0.8.2"
[org.webjars.npm/after "0.8.2"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

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 Licensed

Versions

Version
0.8.2
0.8.1