Skip to content

Commit

Permalink
use of es6, pipeline and concat-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
impressi-web committed Jan 10, 2021
1 parent 96b1230 commit 8bb91a4
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions exercises/reduce/solution/solution.js
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);
}
);

0 comments on commit 8bb91a4

Please sign in to comment.