Manipulate asynchronous operations
Last updated 5 years ago by rstacruz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install async-then 
SYNC missed versions from official npm registry.


Manipulate asynchronous operations

Work with Node-style callbacks in a safe way. The API is modeled after Promises, while async-then doesn't need (or support) promises.

NB: This is a proof-of-concept of applying Promise idioms to callbacks. This package won't likely be supported.



Waterfall flow

Using chain() and then(), you'll be able to run async operations one after the other, giving the result on an operation to the next operation and so on.

function read (symlink, next) {
    .then((_, next)    => { fs.readlink(symlink, next) })
    .then((real, next) => { fs.readdir(real, next) })
    .then((data, next) => { next(null, => path.join(symlink, d)) })

For comparison, here it is without async-then:

function read (path, next) {
  fs.readlink(path, (err, real) => {
    if (err) return next(err)
    fs.readdir(real, (err, data) => {
      if (err) return next(err)
      data = => path.join(symlink, d))

Error control

Notice in the example above, error handling (if (err) return next(err)) is absent. Errors will skip through then() steps, moving onto the next catch() or end() instead.

  .then((_, next) => { fs.lstat(path) })
  .catch((err)    => { if (err !== 'ENOENT') throw err })

At a glance

  • No promises - async-then works with Node-style callbacks (err, result), and does not support promises. It lets you work these kinds of operations in a way you would with Promises/A+ without actually using promises.

  • No wrappers - unlike other solutions like co v3, there's no need to wrap your callback-style functions into thunks or promise-generators.

  • Error catching - no need for extraneous if (err) throw err. Error flow is managed like promises with catch().




Starts a chain. Compare with Promise.resolve() or any other promise. Returns an object with then(), catch() and end() methods.

var chain = require('async-then/chain')

function getTitle (fn) {
    .then((_, next) => { request('', next) })
    .then((data)    => cheerio.load(data))
    .then(($)       => $('title').text()))

getTitle((err, title) => {
  if (err) throw err



Continues a chain; queues up another function to run when previous then() calls complete. In the asynchronous form, the function fn should accept two parameters: result, next.


When fn accepts 2 parameters (result, next), it's invoke asynchronously. The parameter result is the result of the previous operation. next is a function that should be invoked as a callback.

  .then((result, next) => { fs.readFile('url.txt', 'utf-8', next) })
  .then((data, next)   => { request(data, next) })
  .end((err, res)      => { ... })

Synchronous form

When fn only accepts 1 parameter (result), it's invoked synchronously. Whatever its return value will be the value passed to the next then() in the chain.

  .then((result, next) => { fs.readFile('url-list.txt', 'utf-8', next) })
  .then((data)         => { return data.trim().split('\n') })
  .end((err, urls) => {
    /* work with urls */


Th fn function can either throw an error, or invoke next with an error. All errors will skip through the subsequent then() steps; it skips onto the next catch() or end().



Catches errors. It works like then(). If a catch() operation succeeds (meaning it didn't throw an error, or invoke next(err)), it'll continue onto the next then() or end().

  .then((_, next) => { fs.lstat(path) })
  .catch((err)    => { if (err !== 'ENOENT') throw err })



Runs the chain. Without calling .end(fn), the chain will not be called. The parameter fn is a callback that takes Node-style arguments: err, result.

  .then((_, next) => { fs.readFile('data.txt', 'utf-8', next) })
  .end((err, data) => {


all(callbacks, next)

Runs multiple async operations in parallel. Compare with Promise.all().

var all = require('async-then/all')

  next => { request('', next) },
  next => { request('', next) },
  next => { request('', next) }
], (err, results) => {
  // results is an array


async-then © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).  ·  GitHub @rstacruz  ·  Twitter @rstacruz

Current Tags

  • 1.0.1                                ...           latest (5 years ago)

2 Versions

  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
Dev Dependencies (1)

Copyright 2014 - 2017 © |