09 – Exploring Async.js – async.times

Share:
Source Code

In this tutorial, we’re going to learn how we can use async.times to iterate and repeat a particular function.

var async = require('async');

//A complex function to insert entries to database
var addEntryToDB = function(id, callback) {
	//Pass something to first param if it errors out
	callback(null, {
		entryId: id,
		name: 'username' + id
	});
}

//times(n, iterator, [callback])
async.times(5, function(n, next) {
	addEntryToDB(n, function(err, entry) {
		//To simulate error, uncomment the following 3 lines
		if (n === 3) {
			err = 'Someting bad happened';			
		}
		next(err, entry);
	});
}, function(err, entries) {
	if (err) {
		console.log(err);
	}
	//Success, print out entries
	console.log(entries);
});

 

Comments Or Questions? Discuss In Our Discord

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

More from PentaCode