andthen

Async function composition with parameter fixing abiliy.

Async function composition with parameter fixing abiliy.

$ npm install andthen

See Also: comp

Functions almost same as other function composition libraries, except that it lets you bind a property of a produced value to following function.

Simplest usage could look like:

 
andThen = require('andThen')
 
andThen(getContents, 'posts', getPosts, 'images', getImages)(function(errorcontents){
 
    contents.posts
    // => ['Foo', 'bar', 'qux'] 
 
    contents.images
    // => ['11.jpg', '7.jpg', '3.jpg'] 
 
})
 
function getContents(callback){
    callback(, { posts: [3, 1, 4], images: [11, 7, 3])
}
 
function getPosts(idscallback){
    callback(, ['Foo', 'bar', 'qux'])
}
 
function getImages(idscallback){
    callback(, ['11.jpg', '7.jpg', '3.jpg'])
}
 

More detailed example with full functional programming armory:

 
andThen   = require('andThen')
map       = require('map')
partial   = require('new-partial')
 
getPosts  = partial(map, getPost)
getPhotos = partial(map, getPhoto)
 
getProfile = andThen(getUser, 'posts', getPosts, 'photos', getPhotos)
 
getProfile(1, function(errorprofile){
 
    profile.posts[0].title, profile.posts[2].content
    // => Post #1, Content #5 
 
    profile.photos[0].path, profile.photos[1].path
    // => /photos/2.jpg, /photos/7.jpg 
 
})
 
function getUser(idcallback){
 
  callback(, {
      name   : 'Smith',
      posts  : [1, 3, 5],
      photos : [2, 7, 11]
  })
 
}
 
function getPost(idcallback){
 
    callback(, {
        title: 'Post #' + id,
        content: 'Content #' + id
    })
 
}
 
function getPhoto(idcallback){
 
    callback(, {
        path: '/photos' + id + '.jpg
    })
 
}