-
Notifications
You must be signed in to change notification settings - Fork 2
/
beGreeted.js
32 lines (29 loc) · 909 Bytes
/
beGreeted.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
'use strict';
module.exports = function (request, pipe) {
var reply = 'Hello ' + request.body.name;
var array = ['Bob', 'John'];
var responseStream = pipe.streamResponse();
for (var i = 0; i < array.length; i++) {
if (i > 0 && request.body.name === 'timeout-after-first-chunk') {
return;
}
if (request.body.name === 'slow') {
setTimeout(responseStream.write.bind(responseStream, {
message: reply + ' from ' + array[i]
}), 100 * (i + 1));
}
else {
responseStream.write({
message: reply + ' from ' + array[i]
});
}
}
if (request.body.name === 'no-end') {
return;
}
if (request.body.name === 'slow') {
setTimeout(responseStream.end.bind(responseStream), 100 * (i + 1));
return;
}
responseStream.end();
};