-
Notifications
You must be signed in to change notification settings - Fork 1
/
helloworld2.js
47 lines (37 loc) · 1.18 KB
/
helloworld2.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
function say(filename, cb){
//var contents = fs.readFileSync(filename); //read file in synchronous way
//return fs.readFileSync(filename); //read file in synchronous way
// return fs.readFile(filename, function(contents){
// the natural pattern of nodes is to do with the callbacks
// }); // read file in Asynchronous way, with a callback function
//return fs.readFile(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){
var sq = ASQ();
fs.readFile( filename, sq.errfcb() );//error first call backs
return sq;
}
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;