Skip to content

Instantly share code, notes, and snippets.

@w1shen
Last active December 16, 2015 07:49
Show Gist options
  • Save w1shen/5401193 to your computer and use it in GitHub Desktop.
Save w1shen/5401193 to your computer and use it in GitHub Desktop.
learn async.js
var assert = require("assert");
var async = require("async");
var fs = require("fs");
var files = ["node_modules/async/LICENSE", "node_modules/async/package.json"];
async.map(files, fs.stat, function(err, results) {
console.log("map: " + JSON.stringify(results));
});
async.filter(files, fs.exists, function(results) {
assert.deepEqual(files, ["node_modules/async/LICENSE", "node_modules/async/package.json"], "filter");
});
var it = function(item, callback) {
console.log(item);
callback();
};
async.each([1, 2, 3], it, function(err){
console.log("each");
});
async.reduce([1, 2, 3], 0, function(memo, item, callback) {
setImmediate(function() {
callback(null, memo + item);
});
}, function(err, result) {
assert.strictEqual(result, 6, "reduce");
});
async.sortBy(files, function(file, callback) {
fs.stat(file, function(err, stats) {
callback(err, stats.mtime);
});
}, function(err, results) {
assert.deepEqual(files, ["node_modules/async/LICENSE", "node_modules/async/package.json"], "sortBy");
});
async.some(files, fs.exists, function(result) {
assert.strictEqual(result, true, "some");
});
async.every(["node_modules/async/LICENSE", "fail"], fs.exists, function(result) {
assert.strictEqual(result, false, "every");
});
async.concat(["node_modules/async", "node_modules/async/lib",], fs.readdir, function(err, files){
assert.deepEqual(files.sort(), [ "LICENSE", "README.md", "async.js", "lib", "package.json"], "concat");
});
async.series([
function(callback) {
setTimeout(function(){
callback(null, 1);
}, 200);
},
function(callback) {
setTimeout(function(){
callback(null, 2);
}, 100);
}
], function(err, results) {
assert.deepEqual(results, [1, 2], "series");
});
async.parallel([
function(callback) {
setTimeout(function(){
callback(null, 1);
}, 200);
},
function(callback) {
setTimeout(function(){
callback(null, 2);
}, 100);
}
], function(err, results) {
assert.deepEqual(results, [1, 2], "parallel");
});
var i = 0;
async.whilst(function() {
return i < 5;
}, function(callback) {
i++;
setTimeout(callback, 100);
}, function(err) {
assert.strictEqual(i, 5, "whilst");
});
async.waterfall([
function(callback) {
callback(null, 1, 2);
},
function(arg1, arg2, callback) {
assert.strictEqual(arg1, 1, "waterfall");
assert.strictEqual(arg2, 2, "waterfall");
callback(null, 3);
},
function(arg3, callback) {
assert.strictEqual(arg3, 3, "waterfall");
callback(null, "done");
}
], function(err, result) {
assert.strictEqual(result, "done", "waterfall");
});
function add5(n, callback) {
setTimeout(function() {
callback(null, n + 5);
}, 10);
}
function mul5(n, callback) {
setTimeout(function() {
callback(null, n * 5);
}, 10);
}
var a5m5 = async.compose(add5, mul5);
a5m5(5, function(err, result) {
assert.strictEqual(result, 30, "compose");
});
var fun1 = function(str, callback) {
assert.strictEqual(str, "bucket", "applyEach");
callback();
}
var fun2 = function(str, callback) {
assert.strictEqual(str, "bucket", "applyEach");
callback();
}
async.applyEach([fun1, fun2], "bucket", function(err) {
console.log("applyEach");
});
var q = async.queue(function (task, callback) {
assert.strictEqual(task, "task", "queue");
callback();
}, 2);
q.push(["task", "task", "task", "task", "task"], function(err) {
console.log("queue");
});
var c = async.cargo(function (tasks, callback) {
for (var i = 0; i < tasks.length; i++) {
assert.strictEqual(tasks[i], "task", "cargo");
}
callback();
}, 2);
c.push(["task", "task", "task", "task", "task"], function(err) {
console.log("cargo");
});
async.auto({
get1: function(cb) {
cb(null, "data1");
},
get2: function(cb) {
cb(null, "data2");
},
write: ["get1", "get2", function(cb, results) {
assert.deepEqual(results, { get1: "data1", get2: "data2" }, "auto-write");
cb(null, "write");
}],
close: ["write", function(cb, results) {
assert.deepEqual(results, { get1: "data1", get2: "data2", write: "write" }, "auto-end");
cb(null, "close");
}]
}, function(err, results) {
assert.deepEqual(results, { get1: "data1", get2: "data2", write: "write", close: "close" }, "auto");
});
var total = 0;
var it1 = async.iterator([
function() { total++; },
function() { total++; },
function() { total++; }
]);
var it2 = it1();
assert.strictEqual(1, total, "iterator-1");
var it3 = it2();
assert.strictEqual(2, total, "iterator-2");
var it4 = it3();
assert.strictEqual(3, total, "iterator-3");
assert.strictEqual(null, it4, "iterator-4");
var createUser = function(id, callback) {
callback(null, { id: id });
}
async.times(5, function(n, next) {
createUser(n, function(err, user) {
next(err, user);
});
}, function(err, users) {
assert.deepEqual(users, [ { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ], "times");
});
var count = 0;
var slow = function(name, callback) {
count++;
callback(null, name);
}
var fn = async.memoize(slow);
fn("test", function(err, result) {
assert.strictEqual(result, "test", "memoize-1");
assert.strictEqual(count, 1, "memoize-2");
});
fn("test", function(err, result) {
assert.strictEqual(result, "test", "memoize-3");
assert.strictEqual(count, 1, "memoize-4");
});
var fn1 = async.unmemoize(fn);
fn1("test", function(err, result) {
assert.strictEqual(result, "test", "memoize-3");
assert.strictEqual(count, 2, "memoize-4");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment