Permalink
Browse files

Update the do-it-fast article.

  • Loading branch information...
1 parent fb1a6c4 commit ad9a57649d5240c0ef32cd6c7e62925f2b7b7b54 @creationix committed Jul 28, 2010
@@ -1,7 +1,7 @@
Title: "Do" it fast!
Author: Tim Caswell
Date: Mon Feb 22 2010 10:52:08 GMT-0600 (CST)
-Node: v0.1.91
+Node: v0.1.102
Now with the release of [Node v0.1.30][] there is even more need for a library like [Do][]. While working with the node community to decide on the best alternative to node promises, we decided that it's best left to library developers. So as of this morning, node no longer ships with promises, but uses a simple callback interface for all async functions.
@@ -11,13 +11,13 @@ I took my async library that I've been developing throughout the Control Flow se
All async functions in node now use a simple callback based interface:
-<do-it-fast/async.js*>
+<do-it-fast/async.js>
That is, after the arguments, there is a callback function expected. This callback function will be given the error if there was one, and if not, the result after that.
Creating an async function that exports this interface is simple too [plain_callbacks.js][]:
-<do-it-fast/safe-read.js*>
+<do-it-fast/safe-read.js>
These callbacks are fast, simple, and to-the-point. However, your code can get pretty hairy when you start expanding beyond these trivial examples. These simple callback based functions can't be used with aggregate utilities, they can't be implicitly chained or grouped either.
@@ -1,7 +1,6 @@
var fs = require('fs');
-var sys = require('sys');
fs.readdir("/usr", function (err, files) {
if (err) throw err;
- sys.puts("/usr files: " + files);
+ console.log("/usr files: " + files);
});
@@ -1,4 +1,3 @@
-process.mixin(require('sys'));
function errorHandler(error) {
throw error;
@@ -18,4 +17,4 @@ function safeRead(filename) { return function (callback, errback) {
})
}}
-safeRead(__filename)(puts, errorHandler);
+safeRead(__filename)(console.log, errorHandler);
@@ -1,5 +1,3 @@
-process.mixin(require('sys'));
-
function errorHandler(error) {
throw error;
}
@@ -19,7 +17,7 @@ function loadFile(path, callback, errback) {
}
// Pass through the read to regular files as is.
- fs.readFile(path)(callback, errback)
+ fs.readFile(path, 'utf8')(callback, errback)
}, errback);
}
@@ -31,4 +29,4 @@ function loaddir(path) { return function (callback, errback) {
}, errback);
}}
-loaddir(__dirname)(p, errorHandler)
+loaddir(__dirname)(console.dir, errorHandler)
@@ -1,4 +1,3 @@
-process.mixin(require('sys'));
function errorHandler(error) {
throw error;
@@ -28,6 +27,6 @@ safeRead(__filename, function (err, text) {
if (err) {
errorHandler(err);
} else {
- puts(text);
+ console.log(text);
}
});
@@ -1,6 +1,5 @@
// Load 'fs', a built-in node library that has async functions
var fs = require('fs');
-var sys = require('sys');
function safeRead(filename, callback) {
fs.readFile(filename, function (err, data) {
@@ -23,5 +22,5 @@ safeRead(__filename, function (err, text) {
if (err) {
throw err;
}
- sys.puts(text);
+ console.log(text);
})

0 comments on commit ad9a576

Please sign in to comment.