04 – Exploring Async.js – async.whilst and async.forever

Source Code

In this tutorial, we will go over async.whilst with a very simple example of increasing a counter

Whilst will execute the function fn while condition function returns true, it will call callback when the job is done or if any error occurs.

var async = require('async');

var counter = 0;

	function testCondition() { return counter < 5; },
	function increaseCounter(callback) {
		console.log('counter is now', counter);

		//Simulate ajax/processing
		//callback must be called once this function has completed, it takes an optional error argument
		//setTimeout(callback, 1000);
		//so if there's an error, you do callback(err) and it will immediately end.
		setTimeout(callback('err'), 1000);
	function callback(err) {
		if (err) {

		console.log('Job complete');
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