Permalink
Browse files

version 1.2 of book

  • Loading branch information...
1 parent feafcd4 commit ca3d8256528a4419b2d9eaaa08d2a6abdcca34c2 @pgte committed Oct 15, 2012
@@ -0,0 +1,19 @@
+var async = require('async');
+var exists = require('path').exists;
+
+var files = [
+ 'filter.js',
+ 'does_not_exist.js',
+ 'squaring_client.js',
+ 'also_does_not_exist.js',
+ 'squaring_client_limited.js',
+ 'squaring_client_map.js',
+ 'also_does_not_exist_2.js',
+ 'squaring_server.js'
+];
+
+function callback(result) {
+ console.log('this file exists: %s', result);
+}
+
+async.detect(files, exists, callback);
@@ -0,0 +1,19 @@
+var async = require('async');
+var exists = require('path').exists;
+
+var files = [
+ 'filter.js',
+ 'does_not_exist.js',
+ 'squaring_client.js',
+ 'also_does_not_exist.js',
+ 'squaring_client_limited.js',
+ 'squaring_client_map.js',
+ 'also_does_not_exist_2.js',
+ 'squaring_server.js'
+];
+
+function callback(result) {
+ console.log('all these files exist: %j', result);
+}
+
+async.every(files, exists, callback);
@@ -0,0 +1,26 @@
+var myFilter = require('./my_filter');
+
+var request = require('request');
+
+var collection = [1,2,3,4];
+
+function filter(element, next) {
+ process.nextTick(function() {
+ next(null, element % 2 == 0);
+ });
+}
+
+function map(elem, next) {
+ request({ uri: 'http://localhost:4001/',
+ body: elem.toString()},
+ function(err, res, body) {
+ next(err, body && parseInt(body, 10));
+ });
+}
+
+function callback(err, result) {
+ if (err) { console.log(err); }
+ console.log(result);
+}
+
+myFilter(collection, filter, map, callback);
@@ -0,0 +1,23 @@
+var async = require('async');
+var request = require('request');
+
+var items = [];
+var order = process.argv[2] && parseInt(process.argv[2]) || 100;
+var orderMinus2 = order - 2;
+
+for (var i = 0 ; i < orderMinus2 ; i ++) {
+ items.push(i);
+}
+
+var memo = [0, 1];
+
+function iterator(memo, item, next) {
+ request({uri: 'http://localhost:4001/', form: {a: memo[0], b: memo[1]}}, function(err, res, body) {
+ next(err, [memo[1], parseInt(body, 10)]);
+ });
+}
+
+async.reduce(items, memo, iterator, function(err, result) {
+ if (err) { throw err; }
+ console.log('Fibbonacci of order %d: %d', order, result[1]);
+});
@@ -0,0 +1,26 @@
+var async = require('async');
+var exists = require('path').exists;
+
+var files = [
+ 'filter.js',
+ 'does_not_exist.js',
+ 'squaring_client.js',
+ 'also_does_not_exist.js',
+ 'squaring_client_limited.js',
+ 'squaring_client_map.js',
+ 'also_does_not_exist_2.js',
+ 'squaring_server.js'
+];
+
+function callback(result) {
+ console.log('of files %j, these exist: %j', files, result);
+}
+
+function myExists(element, next) {
+ exists(element, function(exists) {
+ console.log(element + ' exists: %j', exists);
+ next(exists);
+ });
+}
+
+async.filter(files, myExists, callback);
@@ -0,0 +1,46 @@
+function myFilter(collection, filter, map, callback) {
+ var calledback = false
+ , results = [];
+
+ function done(err, result) {
+ if (! calledback) {
+ calledback = true;
+ callback(err, result);
+ }
+ }
+
+ var i = 0;
+ var countdown = 0;
+
+ collection.forEach(function(elem) {
+ (function(i) {
+
+ function next(err, passes) {
+ if (err) { return done(err); }
+ if (passes) {
+
+ countdown ++;
+
+ function mapDone(err, result) {
+ if (err) { return done(err); }
+
+ results[i] = result;
+
+ if (-- countdown === 0) {
+ done(null, results);
+ }
+
+ }
+
+ map(elem, mapDone);
+ }
+ }
+
+ filter(elem, next);
+ }(i));
+
+ i ++;
+ });
+}
+
+module.exports = myFilter;
@@ -1,20 +1,25 @@
-var Step = require('step');
+var async = require('async');
+var request = require('request');
-var async = function(order, callback) {
- var timeout = Math.round(Math.random() * 1000);
- setTimeout(function() {
- callback(null, order);
- }, timeout);
-};
+var functions = [];
+
+for ( var i = 0; i < 10; i ++ ) {
+ functions.push(function(i) {
+
+ return function(done) {
+
+ request({uri: 'http://localhost:4001', body: i.toString()}, function(err, res, body) {
+ done(err, body);
+ });
-Step (
- function doAll() {
- for (var i = 0; i < 10; i ++) {
- async(i, this.parallel());
}
- },
- function finalize(err) {
- console.log('done. arguments:');
- console.log(arguments);
- }
-);
+
+ }(i));
+}
+
+function callback(err, result) {
+ if (err) { throw err; }
+ console.log('done: %j', result);
+}
+
+async.parallel(functions, callback);
@@ -0,0 +1,21 @@
+var async = require('async');
+var request = require('request');
+
+function worker(number, done) {
+ request({uri: 'http://localhost:4001/', body: number.toString()}, function(err, res, body) {
+ done(err, number, parseInt(body, 10));
+ });
+}
+
+var queue = async.queue(worker, 5);
+
+for ( var i = 0; i < 1000; i += 1) {
+ queue.push(i, function(err, number, squared) {
+ if (err) {return console.log(err); }
+ console.log('the square of %d is %d', number, squared);
+ });
+}
+
+queue.drain = function() {
+ console.log('queue drained!');
+};
@@ -0,0 +1,19 @@
+var async = require('async');
+var exists = require('path').exists;
+
+var files = [
+ 'filter.js',
+ 'does_not_exist.js',
+ 'squaring_client.js',
+ 'also_does_not_exist.js',
+ 'squaring_client_limited.js',
+ 'squaring_client_map.js',
+ 'also_does_not_exist_2.js',
+ 'squaring_server.js'
+];
+
+function callback(result) {
+ console.log('of files %j, these do not exist: %j', files, result);
+}
+
+async.reject(files, exists, callback);
@@ -0,0 +1,25 @@
+var async = require('async');
+var request = require('request');
+
+var functions = [];
+
+for ( var i = 0; i < 10; i ++ ) {
+ functions.push(function(i) {
+
+ return function(done) {
+
+ request({uri: 'http://localhost:4001', body: i.toString()}, function(err, res, body) {
+ done(err, body);
+ });
+
+ }
+
+ }(i));
+}
+
+function callback(err, result) {
+ if (err) { throw err; }
+ console.log('done: %j', result);
+}
+
+async.series(functions, callback);
View
@@ -0,0 +1,15 @@
+var async = require('async');
+var exists = require('path').exists;
+
+var files = [
+ 'does_not_exist.js',
+ 'also_does_not_exist.js',
+ 'also_does_not_exist_2.js',
+ 'filter.js' ];
+
+
+function callback(result) {
+ console.log('at least one of these files exists: %j', result);
+}
+
+async.some(files, exists, callback);
@@ -0,0 +1,19 @@
+var async = require('async');
+var request = require('request');
+
+var collection = [1, 2, 3, 4];
+
+function iterator(element, next) {
+ request({ uri: 'http://localhost:4001/',
+ body: element.toString() },
+ function(err, res, body) {
+ console.log('square of %d is %d', element, body);
+ next(err);
+ });
+}
+
+function callback() {
+ console.log('finished');
+}
+
+async.forEach(collection, iterator, callback);
@@ -0,0 +1,26 @@
+var async = require('async');
+var request = require('request');
+
+var collection = [];
+
+for (var i = 0; i < 10000; i ++) {
+ collection.push(i);
+}
+
+var maxConcurrency = 5;
+
+function iterator(element, next) {
+ console.log(element);
+ request({ uri: 'http://localhost:4001/',
+ body: element.toString() },
+ function(err, res, body) {
+ //console.log('square of %d is %d', element, body);
+ next(err);
+ });
+}
+
+function callback() {
+ console.log('finished');
+}
+
+async.forEachLimit(collection, maxConcurrency, iterator, callback);
@@ -0,0 +1,23 @@
+var async = require('async');
+var request = require('request');
+
+var collection = [{value:1}, {value: 2}, {value: 3}, { value: 4}];
+
+function iterator(element, next) {
+ request({ uri: 'http://localhost:4001/',
+ body: element.value.toString() },
+ function(err, res, body) {
+ element.result = parseInt(body, 10);
+ next(err);
+ });
+}
+
+function callback(err, result) {
+ console.log('finished.');
+ // for (var i in collection) {
+ console.log(collection);
+ //console.log('the square of %d is %d', collection[i], result[i]);
+ // }
+}
+
+async.forEach(collection, iterator, callback);
@@ -0,0 +1,15 @@
+require('http').createServer(function(req, res) {
+ var body = '';
+
+ req.setEncoding('utf8');
+ req.on('data', function(data) {
+ body += data;
+ });
+
+ req.on('end', function() {
+ var number = parseInt(body, 10);
+ var squared = Math.pow(number, 2);
+ res.end(squared.toString());
+ });
+
+}).listen(4001);
Oops, something went wrong.

0 comments on commit ca3d825

Please sign in to comment.