Skip to content

Instantly share code, notes, and snippets.

@rayterrill
Last active October 13, 2017 04:31
Show Gist options
  • Save rayterrill/fd225fd569a2d62c711750cf0e686e74 to your computer and use it in GitHub Desktop.
Save rayterrill/fd225fd569a2d62c711750cf0e686e74 to your computer and use it in GitHub Desktop.
A simple async NodeJS example
var async = require("async");
function a(callback) {
setTimeout(function() {
console.log('a');
callback();
}, 5000);
}
function b(callback) {
console.log('b');
callback();
}
function done() {
console.log('all done!');
}
async.parallel([
a,
b
], function(err, results) {
done()
});
async = require('async');
//array to hold our tasks
var asyncTasks = [];
function a(callback) {
console.log('called function a!');
callback();
};
function b(callback) {
console.log('called function b!');
callback();
};
function done() {
console.log('done!');
};
//add tasks to the queue
asyncTasks.push(a);
asyncTasks.push(b);
async.parallel(asyncTasks, function() {
//do something once tasks are finished
done();
});
async = require('async');
//import the ad connection logic
var ad = require('./ad.js');
//lookup an account
var sAMAccountName = 'myuser';
//array to hold our tasks
var asyncTasks = [];
function findUser(callback) {
ad.findUser(sAMAccountName, function(err, user) {
if (err) {
callback(err);
return;
}
if (! user) {
var message = 'User: ' + sAMAccountName + ' not found.';
} else {
var message = user.dn;
//console.log(JSON.stringify(user));
}
callback(message);
});
};
function b(callback) {
console.log('called function b!');
callback();
};
function done(data) {
console.log(data);
};
//add tasks to the queue
asyncTasks.push(findUser);
asyncTasks.push(b);
async.parallel(asyncTasks, function(data) {
//do something once tasks are finished
done(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment