-
Notifications
You must be signed in to change notification settings - Fork 1
/
helloworld3.js
64 lines (51 loc) · 1.46 KB
/
helloworld3.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
// Ecosystem around module - how to publish it in the npm
//
function say(filename, cb){
return fs.readFile(filename, function(err, contents){
if(err) {
cb(err);
}
else{
setTimeout(function(){
//cb(err, contents); //as it is in else part so no err
cb(null, contents);
},1000);//for AJAX call or Database connection etc
}
});//putting own wrapper function
}
// function readFile(filename){ // It reads the entire file, not efficient if file is too large
// var sq = ASQ();
//
// fs.readFile( filename, sq.errfcb() );//error first call backs
//
// return sq;
// }
function readFile(filename){
// read file using the stream
return ASQ(function(done){
var stream = fs.createReadStream(filename);
var contents ="";
//Pipe the createReadStream
stream.pipe( fs.createWriteStream(filename+".backup") );
stream.on("data",function(chunk){
//console.log("data"); // temporary logging to be aware that stream loads in chunks
contents += chunk;
});
stream.on("end", function(){
done(contents);
});
});
}
function delayMsg(done, contents){
setTimeout(function(){
done(contents);// done trigger
},1000);
}
function say(filename){
return readFile(filename)
.then(delayMsg);
}
var fs = require("fs");
var ASQ = require("asynquence");
require("asynquence-contrib");
module.exports.say = say;