Sign up for Digital Ocean VPS and get $10 credit FREE!

05 – Exploring Async.js – async.compose1 min read

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);
})

If you enjoyed this tutorial, make sure to subscribe to our Youtube Channel and follow us on Twitter @pentacodevids for latest updates!

Related Posts

Webdev News

Loading...
    More News