From 3ade13179643facb816a24b0413c3ebba414a755 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 31 Aug 2018 15:57:08 -0400 Subject: [PATCH] replace reduce with async reduce; refactor --- src/blockchain.js | 55 ++++++----------------------------------------- src/index.js | 2 +- 2 files changed, 7 insertions(+), 50 deletions(-) diff --git a/src/blockchain.js b/src/blockchain.js index 608c8b8..fdb606b 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -1,40 +1,4 @@ - -function reduce(arr, memo, iteratee, cb) { - if (typeof cb !== 'function') { - if (typeof memo === 'function' && typeof iteratee === 'function') { - cb = iteratee; - iteratee = memo; - memo = []; - } else { - throw new TypeError('expected callback to be a function'); - } - } - - if (!Array.isArray(arr)) { - cb(new TypeError('expected an array')); - return; - } - - if (typeof iteratee !== 'function') { - cb(new TypeError('expected iteratee to be a function')); - return; - } - - (function next(i, acc) { - if (i === arr.length) { - cb(null, acc); - return; - } - - iteratee(acc, arr[i], function(err, val) { - if (err) { - cb(err); - return; - } - next(i + 1, val); - }); - })(0, memo); -}; +import {reduce} from 'async' function isNewWeb3_1() { return (typeof(web3.version) === "string"); @@ -42,14 +6,11 @@ function isNewWeb3_1() { function getAccounts(cb) { if (isNewWeb3_1()) { - web3.eth.getAccounts().then(function(accounts) { - cb(null, accounts); - return null; + return web3.eth.getAccounts().then(function(accounts) { + return cb(null, accounts); }).catch(function(err) { - cb(err); - return null; + return cb(err); }); - return; } web3.eth.getAccounts(cb); }; @@ -60,7 +21,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { const self = this; this.web3 = null; this.doFirst(function(cb) { - reduce(connectionList, function(prev, value, next) { + reduce(connectionList, '', function(prev, value, next) { if (prev === false) { return next(null, false); } @@ -78,11 +39,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { } getAccounts(function(err, account) { - if (err) { - next(null, true) - } else { - next(null, false) - } + return next(null, !!err) }); }, function(err, _result) { self.web3 = web3; diff --git a/src/index.js b/src/index.js index 99864ac..553cd8e 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,7 @@ EmbarkJS.Names = Names; EmbarkJS.Messages = Messages; EmbarkJS.Utils = Utils; EmbarkJS.Contract = function() { - console.error("deprecated: please use EmbarkJS.Blockchain.Contract instead"); + throw new Error('EmbarkJS.Contract is deprecated: please use EmbarkJS.Blockchain.Contract instead'); }; EmbarkJS.isNewWeb3 = Blockchain.Contract.isNewWeb3;