Skip to content
Browse files

Corrently handle function callbacks

  • Loading branch information...
1 parent ff86540 commit 25187a5e7b6588bc895ed5e1c830e3edf8886ba7 @chaoran committed
Showing with 11 additions and 11 deletions.
  1. +1 −2 README.md
  2. +9 −8 index.js
  3. +1 −1 package.json
View
3 README.md
@@ -51,8 +51,7 @@ conn.query = serialize(conn.query, "db");
Current version of `serialize` can only serialize a function that satisfies the following conditions:
1. It accepts a callback function, and invokes the callback when it is done;
-2. If the callback function is optional, the second to last argument cannot be a 'Function' type;
-3. If an error occurs, it passed the error as the first argument to the callback, and the error must be an instance of `Error`.
+2. If an error occurs, it must invoke callback with the error as the first argument; the error must be an instance of `Error`.
Note: Future version of `serialize` may be able to serialize a function that emits `end` and `error` events.
View
17 index.js
@@ -12,18 +12,17 @@ WorkQueue.get = function(name) {
return queue;
};
-WorkQueue.prototype.add = function(_this, func, args) {
+WorkQueue.prototype.add = function(_this, func, args, noCallback) {
var that = this;
- var callback = args.pop();
- if (typeof callback === 'function') {
+ if (noCallback) {
+ args.push(function(err) { that.next(err) });
+ } else {
+ callback = args.pop();
args.push(function() {
callback.apply(this, arguments);
that.next(arguments[0]);
});
- } else {
- args.push(callback);
- args.push(function(err) { that.next(err) });
}
var task = function(err) {
@@ -43,9 +42,11 @@ WorkQueue.prototype.next = function(err) {
var serialize = function(func, name) {
if (!name) name = 'default';
- var queue = WorkQueue.get(name);
+ var queue = WorkQueue.get(name)
+ , length = func.length;
+
var serialized = function() {
- queue.add(this, func, slice.call(arguments));
+ queue.add(this, func, slice.call(arguments), arguments.length < length);
};
serialized.free = function() { return func; };
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "serialize",
- "version": "0.1.0",
+ "version": "0.1.3",
"description": "A simple node utility to serialize execution of asynchronous functions",
"main": "index.js",
"scripts": {

0 comments on commit 25187a5

Please sign in to comment.
Something went wrong with that request. Please try again.