Permalink
Browse files

Update README.md

  • Loading branch information...
1 parent 9723abb commit fe3fa9d7d64442ea7e4d2c29915034acaf6ef0d3 @nakamura-to committed Mar 19, 2012
Showing with 38 additions and 0 deletions.
  1. +38 −0 README.md
View
38 README.md
@@ -211,3 +211,41 @@ parray.forEach(files, function (file) {
});
});
```
+
+## Best Practices
+
+### Wrap Asynchronous Callbacks
+
+Wrap asynchronous callbacks to manage error handling.
+For example, define a following function.
+
+```js
+function bind(callback) {
+ return function (err) {
+ if (err) {
+ err.message += ', callbackLocation: ' + err.callbackLocation;
+ throw err;
+ }
+ callback.apply(null, Array.prototype.slice.call(arguments, 1));
+ }
+}
+```
+
+Use above function as follows:
+
+```js
+var gate = require('gate');
+var fs = require('fs');
+
+var latch = gate.latch();
+fs.readFile('path1', 'utf8', latch(1));
+fs.readFile('path2', 'utf8', latch(1));
+latch.await(bind(function (results) {
+ fs.writeFile('path3', results[0] + results[1], bind(function () {
+ fs.readFile('path3', 'utf8', bind(function (data) {
+ console.log(data);
+ console.log('all done');
+ }));
+ }));
+}));
+```

0 comments on commit fe3fa9d

Please sign in to comment.