JavaScript
で作る
var fs = require("fs"),
path = require("path");
var dirpath = __dirname,
count = 0;
fs.readdir(dirpath, function (err, files) {
files.forEach(function (file) {
var filepath = path.join(dirpath, file);
fs.stat(filepath, function (err, stats) {
if (stats.isFile()) {
count++;
fs.readFile(filepath, function (err, data) {
var copypath = path.join(dirpath, "copy-" + path.basename(filepath));
fs.writeFile(copypath, data, function () {
if (--count === 0) {
console.log("complete");
}
});
});
}
});
});
});
var fs = require("fs"),
path = require("path");
var dirpath = __dirname,
count = 0;
fs.readdir(dirpath, function (err, files) {
files.forEach(function (file) {
var filepath = path.join(dirpath, file);
fs.stat(filepath, function (err, stats) {
if (stats.isFile()) {
count++;
fs.readFile(filepath, function (err, data) {
var copypath = path.join(dirpath, "copy-" + path.basename(filepath));
fs.writeFile(copypath, data, function () {
if (--count === 0) {
console.log("complete");
}
});
});
}
});
});
});
async.waterfall([
function(callback){
callback(null, 'one', 'two');
},
function(arg1, arg2, callback){
callback(null, 'three');
},
function(arg1, callback){
// arg1 now equals 'three'
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'
});
async.parallel([
function(callback){
setTimeout(function(){
callback(null, 'one');
}, 200);
},
function(callback){
setTimeout(function(){
callback(null, 'two');
}, 100);
}
],
// optional callback
function(err, results){
// the results array will equal ['one','two'] even though
// the second function had a shorter timeout.
});
flow.create().flow(function (next) {
next("one", "two");
}).flow(function (next, arg1, arg2) {
next("three");
}).flow(function (next, arg1) {
// arg1 === "three" => true
next("done");
}).flow(function (next, results) {
// results === "done" => true
});
var flowA = flow.create().flow(function (next) {
setTimeout(function () {
next(null, "one");
}, 200);
});
var flowB = flow.create().flow(function (next) {
setTimeout(function () {
next(null, "two");
}, 100);
});
flow.create(flowA, flowB).flow(function (next, arg1, arg2) {
// arg1 === "one" => true
// arg2 === "two" => true
});