05 – Exploring Async.js – async.compose

Share:
Source Code

In this tutorial, we will compose multiple async functions using async.compose

var async = require('async');

//async.compose(fn1,fn2...)
//Composing a(), b(), c() will produce a(b(c()))

//Adds 5 to num
function addFive(num, callback) {
	callback(null, num + 5);
}

//Multiplies num by 10
function timesTen(num, callback) {
	callback(null, num * 10);
}

//Compose calculate(addFive(timesTen(5)))
var calculate = async.compose(addFive, timesTen);

calculate(5, function(err, result) {
	//What do you think result is equal to?
	console.log(result);
});


//seq calculate(addFive(timesTen(5)))
var calculate = async.seq(addFive, timesTen);

calculate(5, function(err, result) {
	//What do you think result is equal to?
	console.log(result);
})

You Might Also Like

Error, group does not exist! Check your syntax! (ID: 7)