Initial async declarations and tests

This commit is contained in:
Boris Yankov
2012-10-22 03:39:43 +03:00
parent 4a655cd2d4
commit 60546f7981
2 changed files with 276 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
// Type definitions for Async 0.1
// Project: https://github.com/caolan/async
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module async {
export interface Errback { (err: string): any; }
// Collections
export function forEach(arr: any[], iterator: (item, callback: Function) => void, callback: Errback): void;
export function forEachSeries(arr: any[], iterator: (item, callback: Function) => void , callback: Errback): void;
export function forEachLimit(arr, limit, iterator, callback);
export function map(arr, iterator, callback);
export function mapSeries(arr, iterator, callback);
export function filter(arr, iterator, callback);
export function filterSeries(arr, iterator, callback);
export function reject(arr, iterator, callback);
export function rejectSeries(arr, iterator, callback);
export function reduce(arr, memo, iterator, callback);
export function reduceRight(arr, memo, iterator, callback);
export function detect(arr, iterator, callback);
export function detectSeries(arr, iterator, callback);
export function sortBy(arr, iterator, callback);
export function some(arr, iterator, callback);
export function every(arr, iterator, callback);
export function concat(arr, iterator, callback);
export function concatSeries(arr, iterator, callback);
// Control Flow
export function series(tasks, callback? );
export function parallel(tasks, callback? );
export function whilst(test, fn, callback);
export function until(test, fn, callback);
export function waterfall(tasks, callback? );
export function queue(worker, concurrency);
export function auto(tasks, callback? );
export function iterator(tasks);
export function apply(fn, ...arguments: any[]);
export function nextTick(callback);
// Utils
export function memoize(fn, hasher? );
export function unmemoize(fn);
export function log(fn, ...arguments: any[]);
export function dir(fn, ...arguments: any[]);
export function noConflict();
}
+228
View File
@@ -0,0 +1,228 @@
/// <reference path="../Definitions/async-0.1.d.ts" />
var fs, path;
//import "async";
async.map(['file1', 'file2', 'file3'], fs.stat, function (err, results) { });
async.filter(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.parallel([
function () { },
function () { }
], callback);
async.series([
function () { },
function () { }
]);
async.map(data, asyncProcess, function (err, results) {
alert(results);
});
async.forEach(openFiles, saveFile, function (err) { });
async.forEachLimit(documents, 20, requestApi, function (err) { });
async.map(['file1', 'file2', 'file3'], fs.stat, function (err, results) { });
async.filter(['file1', 'file2', 'file3'], path.exists, function (results) { });
async.reduce([1, 2, 3], 0, function (memo, item, callback) {
process.nextTick(function () {
callback(null, memo + item)
});
}, function (err, result) { });
async.detect(['file1', 'file2', 'file3'], path.exists, function (result) { });
async.sortBy(['file1', 'file2', 'file3'], function (file, callback) {
fs.stat(file, function (err, stats) {
callback(err, stats.mtime);
});
}, function (err, results) { });
async.some(['file1', 'file2', 'file3'], path.exists, function (result) { });
async.every(['file1', 'file2', 'file3'], path.exists, function (result) { });
async.concat(['dir1', 'dir2', 'dir3'], fs.readdir, function (err, files) { });
async.series([
function (callback) {
callback(null, 'one');
},
function (callback) {
callback(null, 'two');
},
],
function (err, results) { });
async.series({
one: function (callback) {
setTimeout(function () {
callback(null, 1);
}, 200);
},
two: function (callback) {
setTimeout(function () {
callback(null, 2);
}, 100);
},
},
function (err, results) { });
async.parallel([
function (callback) {
setTimeout(function () {
callback(null, 'one');
}, 200);
},
function (callback) {
setTimeout(function () {
callback(null, 'two');
}, 100);
},
],
function (err, results) { });
async.parallel({
one: function (callback) {
setTimeout(function () {
callback(null, 1);
}, 200);
},
two: function (callback) {
setTimeout(function () {
callback(null, 2);
}, 100);
},
},
function (err, results) { });
var count = 0;
async.whilst(
function () { return count < 5; },
function (callback) {
count++;
setTimeout(callback, 1000);
},
function (err) { }
);
async.waterfall([
function (callback) {
callback(null, 'one', 'two');
},
function (arg1, arg2, callback) {
callback(null, 'three');
},
function (arg1, callback) {
callback(null, 'done');
}
], function (err, result) { });
var q = async.queue(function (task, callback) {
console.log('hello ' + task.name);
callback();
}, 2);
q.drain = function () {
console.log('all items have been processed');
}
q.push({ name: 'foo' }, function (err) {
console.log('finished processing foo');
});
q.push({ name: 'bar' }, function (err) {
console.log('finished processing bar');
});
q.push([{ name: 'baz' }, { name: 'bay' }, { name: 'bax' }], function (err) {
console.log('finished processing bar');
});
async.auto({
get_data: function (callback) { },
make_folder: function (callback) { },
write_file: ['get_data', 'make_folder', function (callback) {
callback(null, filename);
}],
email_link: ['write_file', function (callback, results) { }]
});
async.parallel([
function (callback) { },
function (callback) { }
],
function (results) {
async.series([
function (callback) {
},
email_link: function(callback) { }
]);
});
var iterator = async.iterator([
function () { sys.p('one'); },
function () { sys.p('two'); },
function () { sys.p('three'); }
]);
async.parallel([
async.apply(fs.writeFile, 'testfile1', 'test1'),
async.apply(fs.writeFile, 'testfile2', 'test2'),
]);
async.parallel([
function (callback) {
fs.writeFile('testfile1', 'test1', callback);
},
function (callback) {
fs.writeFile('testfile2', 'test2', callback);
},
]);
var call_order = [];
async.nextTick(function () {
call_order.push('two');
// call_order now equals ['one','two]
});
call_order.push('one');
var slow_fn = function (name, callback) {
callback(null, result);
};
var fn = async.memoize(slow_fn);
// fn can now be used as if it were slow_fn
fn('some name', function () {
// callback
});
var hello = function (name, callback) {
setTimeout(function () {
callback(null, 'hello ' + name);
}, 1000);
};
var hello = function (name, callback) {
setTimeout(function () {
callback(null, { hello: name });
}, 1000);
};