An async function composition allows you to combine multiple asynchronous functions into a single function.
Any errors from any of the functions will go to the domain.
var compose = require("compose-async-domain")
, assert = require("assert")
, Domain = require("domain").create
var domainOne = Domain()
, one = compose(domainOne
, function third(arg, callback) {
callback(null, arg * 2)
}
, function second(arg, callback) {
callback(null, arg + 2)
}
, function first(arg, callback) {
callback(null, arg * 2)
})
domainOne.on("error", function (err) {
// there are no errors
assert.ok(false, "should never fire")
})
one(2, function (result) {
assert.equal(result, 12)
})
var domainTwo = Domain()
, two = compose(domainTwo
, function third(data, callback) {
if (data === "") {
return callback(new Error("empty :("))
}
callback(null, data)
}
, function second(data, callback) {
callback(null, data.toString().toUpperCase())
}
, function first(filePath, callback) {
fs.readFile(filePath, callback)
})
domainTwo.on("error", function (err) {
// error from empty file
assert.equal(err.message, "empty :(")
})
two("sensibleFile.txt", function (data) {
// upper cased data
})
two("emptyFile.txt", function () {
// never gets called
})