Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "kgo", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Flow control the super easy way", | ||
"main": "kgo.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test" | ||
}, | ||
@@ -9,0 +9,0 @@ "author": "", |
@@ -20,3 +20,3 @@ kgo | ||
kgo returns itsself, so it can be chained: | ||
kgo returns its-self, so it can be chained: | ||
@@ -70,2 +70,22 @@ kgo(name, deps, fn)(name, deps, fn)(name, deps, fn) | ||
You can do an async map over items by setting the count of the items in the callback: | ||
var items = [1,2,3,4]; | ||
kgo('items', function(cb){ | ||
cb(null, items); | ||
})('doubled', ['items'], function(items, cb){ | ||
// Here we tell kgo that we will be returning an array of results, not just one. | ||
this.count(items.length); | ||
for(var i = 0; i < items.length; i++){ | ||
// Call the callback as usual, but make sure it is called as many times as you said it would be. | ||
cb(null, items[i]*2); | ||
} | ||
})(['doubled'], function(doubled){ | ||
// Our doubled numbers | ||
}); | ||
## Errors | ||
@@ -72,0 +92,0 @@ |
39
run.js
@@ -0,1 +1,35 @@ | ||
function Step(task, args, done){ | ||
this._task = task; | ||
this._args = args; | ||
this._done = done; | ||
} | ||
Step.prototype._count = 1; | ||
Step.prototype._runs = 0; | ||
Step.prototype.run = function(){ | ||
var step = this, | ||
results = [], | ||
didError; | ||
this._task.fn.apply(this, this._args.concat([function(error, result){ | ||
results.push(result); | ||
step._runs++; | ||
if(error){ | ||
didError = true; | ||
step.done(error); | ||
}else if(!didError && step._runs === step._count){ | ||
step.done(null, results); | ||
} | ||
}])); | ||
}; | ||
Step.prototype.count = function(number){ | ||
this._parallel = true; | ||
this._count = number; | ||
}; | ||
Step.prototype.done = function(error, results){ | ||
if(error){ | ||
return this._done(error); | ||
} | ||
this._done(null, this._parallel ? results : results[0]); | ||
}; | ||
function runTask(task, results, aboutToRun, done){ | ||
@@ -17,3 +51,3 @@ var name = task.name, | ||
args.push(function(error, result){ | ||
var step = new Step(task, args, function(error, result){ | ||
done(name, error, result); | ||
@@ -23,4 +57,3 @@ }); | ||
aboutToRun(name); | ||
taskFunction.apply(null, args); | ||
step.run(); | ||
} | ||
@@ -27,0 +60,0 @@ |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
7563
7
182
2
102