-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use of es6, pipeline and concat-stream
- Loading branch information
1 parent
96b1230
commit 8bb91a4
Showing
1 changed file
with
27 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
var http = require('http') | ||
, async = require('async'); | ||
"use strict"; | ||
const { get } = require("http"); | ||
const async = require("async"); | ||
const { pipeline } = require("stream"); | ||
const concat = require("concat-stream"); | ||
|
||
async.reduce(['one', 'two', 'three'], 0, function(memo, item, done){ | ||
var body = ''; | ||
|
||
http.get(process.argv[2] + "?number=" + item, function(res){ | ||
res.on('data', function(chunk){ | ||
body += chunk.toString(); | ||
}); | ||
|
||
res.on('end', function(){ | ||
done(null, memo + Number(body)); | ||
}); | ||
}).on('error', done); | ||
|
||
}, function done(err, result){ | ||
if (err) return console.log(err); | ||
console.log(result); | ||
}); | ||
async.reduce( | ||
["one", "two", "three"], | ||
0, | ||
function (memo, item, done) { | ||
get(process.argv[2] + "?number=" + item, (res) => { | ||
res.setEncoding("utf-8"); | ||
pipeline( | ||
res, | ||
concat((data) => { | ||
done(null, Number(data) + memo); | ||
}), | ||
(err) => { | ||
if (err) return done(err); | ||
} | ||
); | ||
}).on("error", done); | ||
}, | ||
function (err, result) { | ||
if (err) return console.error(err); | ||
console.log(result); | ||
} | ||
); |