-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfiles.js
64 lines (60 loc) · 1.61 KB
/
files.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
57
58
59
60
61
62
63
64
// This run function will correctly handle continouables
// that proxy to api's that have the node style of cps api
//
function run(genfun){
var gen = genfun()
// This is the async loop pattern <http://tobyho.com/2011/11/03/delaying-repeatedly/>
function next(err, answer){
var res
if (err){
// if error, we want to throw it so it's catchable
// on the outside
return gen.throw.apply(gen, arguments)
}else{
// if no error, send the answer back to the outside
res = gen.next.call(gen, answer)
}
if (!res.done){
// recursive call to continues the loop
res.value(next)
}
}
next() // start the loop
}
// wrap a node-style cps function as a continuable
function wrap(fun){
return function(){
var args = Array.prototype.slice.apply(arguments)
return function(callback){
args.push(callback)
fun.apply(null, args)
}
}
}
// wrap an entire node module by proxying all the node-style
// apis via continuables
function wrapModule(module){
var retval = {}
for (var key in module){
var fun = module[key]
if (typeof fun === 'function'){
retval[key] = wrap(fun)
}
}
return retval
}
// wrap the fs module
var fs = wrapModule(require('fs'))
// Now do a bunch of file operations
run(function*(){
try{
var file = yield fs.readFile('files.js')
console.log(file.toString())
var stat = yield fs.stat('files.js')
console.log(stat)
stat = yield fs.stat('flies.js') // this should trigger error because file no exist
console.log(stat)
}catch(e){
console.log('Gracefully handling error', e.message)
}
})