Skip to content

Commit

Permalink
async: Corrected and updated to 1.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Herman committed Aug 24, 2015
1 parent af81415 commit 375b761
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 145 deletions.
137 changes: 121 additions & 16 deletions async/async-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ var fs, path;
function callback() {}

async.map(['file1', 'file2', 'file3'], fs.stat, function (err, results) { });
async.mapSeries(['file1', 'file2', 'file3'], fs.stat, function (err, results) { });
async.mapLimit(['file1', 'file2', 'file3'], 2, fs.stat, function (err, results) { });

async.filter(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.filterSeries(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.filterLimit(['file1', 'file2', 'file3'], 2, path.exists, function (results) { });
async.select(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.selectSeries(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.selectLimit(['file1', 'file2', 'file3'], 2, path.exists, function (results) { });

async.reject(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.rejectSeries(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.rejectLimit(['file1', 'file2', 'file3'], 2, path.exists, function (results) { });

async.parallel([
function () { },
Expand All @@ -25,25 +36,46 @@ async.map(data, asyncProcess, function (err, results) {
});

var openFiles = ['file1', 'file2'];
var openFilesObj = {
file1: "fileOne",
file2: "fileTwo"
}

var saveFile = function () { }
async.each(openFiles, saveFile, function (err) { });
async.eachSeries(openFiles, saveFile, function (err) { });

var documents, requestApi;
async.eachLimit(documents, 20, requestApi, function (err) { });

async.map(['file1', 'file2', 'file3'], fs.stat, function (err, results) { });

async.filter(['file1', 'file2', 'file3'], path.exists, function (results) { });
// forEachOf* functions. May accept array or object.
function forEachOfIterator(item, key, forEachOfIteratorCallback) {
console.log("ForEach: item=" + item + ", key=" + key);
forEachOfIteratorCallback();
}
async.forEachOf(openFiles, forEachOfIterator, function (err) { });
async.forEachOf(openFilesObj, forEachOfIterator, function (err) { });
async.forEachOfSeries(openFiles, forEachOfIterator, function (err) { });
async.forEachOfSeries(openFilesObj, forEachOfIterator, function (err) { });
async.forEachOfLimit(openFiles, 2, forEachOfIterator, function (err) { });
async.forEachOfLimit(openFilesObj, 2, forEachOfIterator, function (err) { });

var process;
async.reduce([1, 2, 3], 0, function (memo, item, callback) {
var numArray = [1, 2, 3];
function reducer(memo, item, callback) {
process.nextTick(function () {
callback(null, memo + item)
});
}, function (err, result) { });
}
async.reduce(numArray, 0, reducer, function (err, result) { });
async.inject(numArray, 0, reducer, function (err, result) { });
async.foldl(numArray, 0, reducer, function (err, result) { });
async.reduceRight(numArray, 0, reducer, function (err, result) { });
async.foldr(numArray, 0, reducer, function (err, result) { });

async.detect(['file1', 'file2', 'file3'], path.exists, function (result) { });
async.detectSeries(['file1', 'file2', 'file3'], path.exists, function (result) { });
async.detectLimit(['file1', 'file2', 'file3'], 2, path.exists, function (result) { });

async.sortBy(['file1', 'file2', 'file3'], function (file, callback) {
fs.stat(file, function (err, stats) {
Expand All @@ -52,10 +84,18 @@ async.sortBy(['file1', 'file2', 'file3'], function (file, callback) {
}, function (err, results) { });

async.some(['file1', 'file2', 'file3'], path.exists, function (result) { });
async.someLimit(['file1', 'file2', 'file3'], 2, path.exists, function (result) { });
async.any(['file1', 'file2', 'file3'], path.exists, function (result) { });

async.every(['file1', 'file2', 'file3'], path.exists, function (result) { });
async.everyLimit(['file1', 'file2', 'file3'], 2, path.exists, function (result) { });
async.all(['file1', 'file2', 'file3'], path.exists, function (result) { });

async.concat(['dir1', 'dir2', 'dir3'], fs.readdir, function (err, files) { });
async.concatSeries(['dir1', 'dir2', 'dir3'], fs.readdir, function (err, files) { });


// Control Flow //

async.series([
function (callback) {
Expand All @@ -77,7 +117,6 @@ async.series<string>([
],
function (err, results) { });


async.series({
one: function (callback) {
setTimeout(function () {
Expand Down Expand Up @@ -173,21 +212,47 @@ async.parallel<number>({
}, 100);
},
},
function (err, results) { });


var count = 0;
function (err, results) { });

async.whilst(
function () { return count < 5; },
function (callback) {
count++;
setTimeout(callback, 1000);
async.parallelLimit({
one: function (callback) {
setTimeout(function () {
callback(null, 1);
}, 200);
},
two: function (callback) {
setTimeout(function () {
callback(null, 2);
}, 100);
},
function (err) { }
},
2,
function (err, results) { }
);


function whileFn(callback) {
count++;
setTimeout(callback, 1000);
}

function whileTest() { return count < 5; }
var count = 0;
async.whilst(whileTest, whileFn, function (err) { });
async.until(whileTest, whileFn, function (err) { });
async.doWhilst(whileFn, whileTest, function (err) { });
async.doUntil(whileFn, whileTest, function (err) { });

async.during(function (testCallback) { testCallback(new Error(), false); }, function (callback) { callback() }, function (error) { console.log(error) });
async.doDuring(function (callback) { callback() }, function (testCallback) { testCallback(new Error(), false); }, function (error) { console.log(error) });
async.forever(function (errBack) {
errBack(new Error("Not going on forever."));
},
function (error) {
console.log(error);
}
);

async.waterfall([
function (callback) {
callback(null, 'one', 'two');
Expand Down Expand Up @@ -279,6 +344,26 @@ q2.unshift(['task3', 'task4', 'task5'], function (error) {
console.log('Finished tasks');
});

// create a cargo object with payload 2
var cargo = async.cargo(function (tasks, callback) {
for (var i = 0; i < tasks.length; i++) {
console.log('hello ' + tasks[i].name);
}
callback();
}, 2);


// add some items
cargo.push({ name: 'foo' }, function (err) {
console.log('finished processing foo');
});
cargo.push({ name: 'bar' }, function (err) {
console.log('finished processing bar');
});
cargo.push({ name: 'baz' }, function (err) {
console.log('finished processing baz');
});

var filename = '';
async.auto({
get_data: function (callback) { },
Expand All @@ -291,6 +376,9 @@ async.auto({
email_link: ['write_file', <any>function (callback, results) { }]
});

async.retry(3, function (callback, results) { }, function (err, result) { });
async.retry({ times: 3, interval: 200 }, function (callback, results) { }, function (err, result) { });


async.parallel([
function (callback) { },
Expand Down Expand Up @@ -336,3 +424,20 @@ var slow_fn = function (name, callback) {
};
var fn = async.memoize(slow_fn);
fn('some name', function () {});
async.unmemoize(fn);
async.ensureAsync(function () { });
async.constant(42);
async.asyncify(function () { });

async.log(function (name, callback) {
setTimeout(function () {
callback(null, 'hello ' + name);
}, 0);
}, "world"
);

async.dir(function (name, callback) {
setTimeout(function () {
callback(null, { hello: name });
}, 1000);
}, "world");
Loading

0 comments on commit 375b761

Please sign in to comment.