-
Notifications
You must be signed in to change notification settings - Fork 1
/
async.js
56 lines (45 loc) · 890 Bytes
/
async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const async = require('async')
/*
instruments async for sweet deets
*/
module.exports = {
series: series,
parallel: parallel,
}
function series(obj, cb){
var newTasks = wrapTasks(obj)
async.series(newTasks, cb)
}
function parallel(obj, cb){
var newTasks = wrapTasks(obj)
async.parallel(newTasks, cb)
}
function wrapTasks(obj){
var newTasks = {}
keyValuesFor(obj).map(function(task){
var label = task.key
var taskFn = task.value
newTasks[label] = function(cb){
onStart()
taskFn(function(){
onEnd()
cb.apply(null, arguments)
})
}
function onStart(){
console.log(label, 'start')
}
function onEnd(){
console.log(label, 'end')
}
})
return newTasks
}
function keyValuesFor(obj){
return Object.keys(obj).map(function(key){
return {
key,
value: obj[key],
}
})
}