mirror of
https://github.com/embarklabs/EmbarkJS.git
synced 2025-02-19 23:58:16 +00:00
replace reduce with async reduce; refactor
This commit is contained in:
parent
b85f5d7e89
commit
3ade131796
@ -1,40 +1,4 @@
|
|||||||
|
import {reduce} from 'async'
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
function isNewWeb3_1() {
|
function isNewWeb3_1() {
|
||||||
return (typeof(web3.version) === "string");
|
return (typeof(web3.version) === "string");
|
||||||
@ -42,14 +6,11 @@ function isNewWeb3_1() {
|
|||||||
|
|
||||||
function getAccounts(cb) {
|
function getAccounts(cb) {
|
||||||
if (isNewWeb3_1()) {
|
if (isNewWeb3_1()) {
|
||||||
web3.eth.getAccounts().then(function(accounts) {
|
return web3.eth.getAccounts().then(function(accounts) {
|
||||||
cb(null, accounts);
|
return cb(null, accounts);
|
||||||
return null;
|
|
||||||
}).catch(function(err) {
|
}).catch(function(err) {
|
||||||
cb(err);
|
return cb(err);
|
||||||
return null;
|
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
web3.eth.getAccounts(cb);
|
web3.eth.getAccounts(cb);
|
||||||
};
|
};
|
||||||
@ -60,7 +21,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) {
|
|||||||
const self = this;
|
const self = this;
|
||||||
this.web3 = null;
|
this.web3 = null;
|
||||||
this.doFirst(function(cb) {
|
this.doFirst(function(cb) {
|
||||||
reduce(connectionList, function(prev, value, next) {
|
reduce(connectionList, '', function(prev, value, next) {
|
||||||
if (prev === false) {
|
if (prev === false) {
|
||||||
return next(null, false);
|
return next(null, false);
|
||||||
}
|
}
|
||||||
@ -78,11 +39,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getAccounts(function(err, account) {
|
getAccounts(function(err, account) {
|
||||||
if (err) {
|
return next(null, !!err)
|
||||||
next(null, true)
|
|
||||||
} else {
|
|
||||||
next(null, false)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, function(err, _result) {
|
}, function(err, _result) {
|
||||||
self.web3 = web3;
|
self.web3 = web3;
|
||||||
|
@ -21,7 +21,7 @@ EmbarkJS.Names = Names;
|
|||||||
EmbarkJS.Messages = Messages;
|
EmbarkJS.Messages = Messages;
|
||||||
EmbarkJS.Utils = Utils;
|
EmbarkJS.Utils = Utils;
|
||||||
EmbarkJS.Contract = function() {
|
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;
|
EmbarkJS.isNewWeb3 = Blockchain.Contract.isNewWeb3;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user